#include<iostream> #include<cstdio> #include<cmath> #include<vector> #include<queue> #include<cstring> using namespace std; int main() { //freopen("in.txt" , "r", stdin); string a; long long b, rem; int test, t, i; cin>> test; for(t=1; t<=test; t++) { cin >> a >> b; if(b<0) b = b*(-1); int len = a.length(); rem=0; for(i=0; i<len; i++) { if(a[i]=='-') i=1; rem*=10; rem+=(a[i]-'0'); rem%=b; } if(rem==0) printf("Case %d: divisible\n", t); else printf("Case %d: not divisible\n", t); } }
Friday, October 9, 2015
1214 - Large Division (LightOJ)
Saturday, September 12, 2015
1082 - Array Queries (Light OJ)
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
#define MX 10005
const int N = 100000;
int data[N+10], save[4*N+10];
int inf = 1 << 28;
int n;
void build_tree(int node, int st, int en, int a[], int A[])
{
if(st==en)
{
A[node] = a[st];
return;
}
int mid = st+ ((en-st)>>1);
int left = node<<1;
int right = (node<<1)+1;
build_tree(left, st, mid, a, A);
build_tree(right, mid+1, en, a, A);
A[node] = min(A[left], A[right]);
}
int query_tree(int node, int st, int en, int a, int b, int A[])
{
if(a>b)
return inf;
if(st==a && en==b)
return A[node];
int mid = st+ ((en-st)>>1);
int left = node<<1;
int right = (node<<1)+1;
int l = query_tree(left, st, mid, a, min(b, mid), A);
int r = query_tree(right ,mid+1, en, max(a, mid+1), b, A);
return min(l, r);
}
void Build(int tot)
{
build_tree(1, 1, tot, data, save);
}
int query(int a, int b)
{
return query_tree(1, 1, n, a, b, save);
}
int main()
{
//freopen("in.txt", "r", stdin);
int test, t;
int q, a, b;
cin >> test;
for(t=1; t<=test; t++)
{
cin >> n >> q;
for(int i=1; i<=n; i++)
scanf("%d", &data[i]);
Build(n);
cout << "Case " << t << ":" << endl;
for(int i=1; i<=q; i++)
{
scanf("%d %d", &a, &b);
printf("%d\n", query(a, b));
}
}
return 0;
}
#include<cstdio>
#include<cmath>
using namespace std;
#define MX 10005
const int N = 100000;
int data[N+10], save[4*N+10];
int inf = 1 << 28;
int n;
void build_tree(int node, int st, int en, int a[], int A[])
{
if(st==en)
{
A[node] = a[st];
return;
}
int mid = st+ ((en-st)>>1);
int left = node<<1;
int right = (node<<1)+1;
build_tree(left, st, mid, a, A);
build_tree(right, mid+1, en, a, A);
A[node] = min(A[left], A[right]);
}
int query_tree(int node, int st, int en, int a, int b, int A[])
{
if(a>b)
return inf;
if(st==a && en==b)
return A[node];
int mid = st+ ((en-st)>>1);
int left = node<<1;
int right = (node<<1)+1;
int l = query_tree(left, st, mid, a, min(b, mid), A);
int r = query_tree(right ,mid+1, en, max(a, mid+1), b, A);
return min(l, r);
}
void Build(int tot)
{
build_tree(1, 1, tot, data, save);
}
int query(int a, int b)
{
return query_tree(1, 1, n, a, b, save);
}
int main()
{
//freopen("in.txt", "r", stdin);
int test, t;
int q, a, b;
cin >> test;
for(t=1; t<=test; t++)
{
cin >> n >> q;
for(int i=1; i<=n; i++)
scanf("%d", &data[i]);
Build(n);
cout << "Case " << t << ":" << endl;
for(int i=1; i<=q; i++)
{
scanf("%d %d", &a, &b);
printf("%d\n", query(a, b));
}
}
return 0;
}
Wednesday, September 9, 2015
490B - Queue
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<vector>
#include<fstream>
#include<map>
using namespace std;
#define sc(n) scanf("%d", &n)
#define sf scanf
#define pf printf
#define MX 100005
#define pb push_back
#define ff first
#define ss second
typedef long long ll;
int freq[1000005];
int arr[200005];
vector < pair<int, int> > VP;
map<int , int> mp;
int main()
{
//freopen("in4.txt","r",stdin);
int i, j, a, b, n;
int check, check2, inEven=2, inOdd=1;
cin >> n;
for(i=0; i<n; i++)
{
cin >> a >> b;
VP.pb( make_pair(a, b));
mp[a] = b;
freq[VP[i].ff]++;
freq[VP[i].ss]++;
if(VP[i].ff==0)
{
check = VP[i].ss;
arr[inEven] = check;
inEven+=2;
}
}
for(int i=0; i<n; i++)
{
if(freq[VP[i].ff]==1)
{
check2 = VP[i].ff;
arr[inOdd] = check2;
inOdd+=2;
}
}
for(int i=0; i<=(n/2); i++)
{
arr[inEven] = mp[check] ;
check = arr[inEven];
inEven+=2;
arr[inOdd] = mp[check2];
check2 = arr[inOdd] ;
inOdd+=2;
}
for(int i=1; i<=n; i++)
cout << arr[i] << " " ;
return 0;
}
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<vector>
#include<fstream>
#include<map>
using namespace std;
#define sc(n) scanf("%d", &n)
#define sf scanf
#define pf printf
#define MX 100005
#define pb push_back
#define ff first
#define ss second
typedef long long ll;
int freq[1000005];
int arr[200005];
vector < pair<int, int> > VP;
map<int , int> mp;
int main()
{
//freopen("in4.txt","r",stdin);
int i, j, a, b, n;
int check, check2, inEven=2, inOdd=1;
cin >> n;
for(i=0; i<n; i++)
{
cin >> a >> b;
VP.pb( make_pair(a, b));
mp[a] = b;
freq[VP[i].ff]++;
freq[VP[i].ss]++;
if(VP[i].ff==0)
{
check = VP[i].ss;
arr[inEven] = check;
inEven+=2;
}
}
for(int i=0; i<n; i++)
{
if(freq[VP[i].ff]==1)
{
check2 = VP[i].ff;
arr[inOdd] = check2;
inOdd+=2;
}
}
for(int i=0; i<=(n/2); i++)
{
arr[inEven] = mp[check] ;
check = arr[inEven];
inEven+=2;
arr[inOdd] = mp[check2];
check2 = arr[inOdd] ;
inOdd+=2;
}
for(int i=1; i<=n; i++)
cout << arr[i] << " " ;
return 0;
}
Monday, September 7, 2015
488B - Candy Boxes (Codeforces)
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;
#define sc(n) scanf("%d", &n)
#define sf scanf
#define pf printf
#define MX 100005
typedef long long ll;
float arr[10];
bool check(float a, float b, float c, float d)
{
arr[0] = a, arr[1] = b, arr[2]= c, arr[3] = d;
sort(arr, arr+4);
a = arr[0], b = arr[1], c = arr[2], d = arr[3];
float am, median, range;
am = (a+b+c+d)/4;
median = (b+c)/2;
range = d-a;
if(am==median && median==range)
return true;
else
return false;
}
int main()
{
//freopen("in.txt","r",stdin);
int n, i, j, k, lost, flag=0;
float x1, x2, x3, x4;
cin >> n;
lost = 4-n;
if(lost==0)
{
cin >> x1 >> x2 >> x3 >> x4;
if(check(x1, x2, x3, x4))
cout << "YES" << endl;
else
cout << "NO" << endl;
}
else if(lost==1)
{
cin >> x1 >> x2 >> x3;
for(i=1; i<=1500; i++)
{
if(check(x1, x2, x3, i))
x4 = i, flag=1;
}
if(flag==1)
cout << "YES" << endl << x4 << endl;
else
cout << "NO" << endl;
}
else if(lost==2)
{
cin >> x1 >> x2;
for(i=1; i<=1500; i++)
{
for(j=1; j<=1500; j++)
{
if(check(x1, x2, i, j))
x3 =i , x4 = j, flag=1; continue;
}
}
if(flag==1)
cout << "YES" << endl << x3 << endl << x4 << endl;
else
cout << "NO" << endl;
}
else if(lost==3)
{
cin >> x1;
x2 = x1, x3 =3*x1 , x4 = x3;
if(check(x1, x2, x3, x4))
cout << "YES" << endl << x2 << endl << x3 << endl << x4 << endl;
else
cout << "NO" << endl;
}
else if(lost==4)
{
cout << "YES" << endl << "1" << endl << "1" << endl;
cout << "3" << endl << "3" << endl;
}
return 0;
}
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;
#define sc(n) scanf("%d", &n)
#define sf scanf
#define pf printf
#define MX 100005
typedef long long ll;
float arr[10];
bool check(float a, float b, float c, float d)
{
arr[0] = a, arr[1] = b, arr[2]= c, arr[3] = d;
sort(arr, arr+4);
a = arr[0], b = arr[1], c = arr[2], d = arr[3];
float am, median, range;
am = (a+b+c+d)/4;
median = (b+c)/2;
range = d-a;
if(am==median && median==range)
return true;
else
return false;
}
int main()
{
//freopen("in.txt","r",stdin);
int n, i, j, k, lost, flag=0;
float x1, x2, x3, x4;
cin >> n;
lost = 4-n;
if(lost==0)
{
cin >> x1 >> x2 >> x3 >> x4;
if(check(x1, x2, x3, x4))
cout << "YES" << endl;
else
cout << "NO" << endl;
}
else if(lost==1)
{
cin >> x1 >> x2 >> x3;
for(i=1; i<=1500; i++)
{
if(check(x1, x2, x3, i))
x4 = i, flag=1;
}
if(flag==1)
cout << "YES" << endl << x4 << endl;
else
cout << "NO" << endl;
}
else if(lost==2)
{
cin >> x1 >> x2;
for(i=1; i<=1500; i++)
{
for(j=1; j<=1500; j++)
{
if(check(x1, x2, i, j))
x3 =i , x4 = j, flag=1; continue;
}
}
if(flag==1)
cout << "YES" << endl << x3 << endl << x4 << endl;
else
cout << "NO" << endl;
}
else if(lost==3)
{
cin >> x1;
x2 = x1, x3 =3*x1 , x4 = x3;
if(check(x1, x2, x3, x4))
cout << "YES" << endl << x2 << endl << x3 << endl << x4 << endl;
else
cout << "NO" << endl;
}
else if(lost==4)
{
cout << "YES" << endl << "1" << endl << "1" << endl;
cout << "3" << endl << "3" << endl;
}
return 0;
}
492B - Vanya and Lanterns (Codeforces)
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;
#define sc(n) scanf("%d", &n)
#define sf scanf
#define pf printf
#define MX 10005
typedef long long ll;
double a[MX];
int main()
{
//freopen("in.txt","r",stdin);
int i, n, l;
double ans1, ans2, d, mx;
cin >> n >> l;
for(i=0; i<n; i++)
cin >> a[i];
sort(a, a+n);
ans1 = max(a[0]-0, l-a[n-1]);
mx =0;
for(i=1; i<n; i++)
{
d = (a[i]-a[i-1])/2;
if(d>mx)
mx = d;
}
ans2 = max(ans1, mx);
pf("%.10f\n", ans2);
return 0;
}
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;
#define sc(n) scanf("%d", &n)
#define sf scanf
#define pf printf
#define MX 10005
typedef long long ll;
double a[MX];
int main()
{
//freopen("in.txt","r",stdin);
int i, n, l;
double ans1, ans2, d, mx;
cin >> n >> l;
for(i=0; i<n; i++)
cin >> a[i];
sort(a, a+n);
ans1 = max(a[0]-0, l-a[n-1]);
mx =0;
for(i=1; i<n; i++)
{
d = (a[i]-a[i-1])/2;
if(d>mx)
mx = d;
}
ans2 = max(ans1, mx);
pf("%.10f\n", ans2);
return 0;
}
Sunday, September 6, 2015
459B - Pashmak and Flowers (Codeforces)
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;
#define sc(n) scanf("%d", &n)
#define sf scanf
#define pf printf
#define MX 1000005
typedef long long ll;
ll a[MX];
int main()
{
int n, i;
ll ans1, ans2, cnt1, cnt2;
while(sc(n)==1)
{
for(i=0; i<n; i++)
scanf("%lld", &a[i]);
sort(a, a+n);
cnt1 = cnt2= 0;
for(i=0; i<n; i++)
{
ans1 = a[n-1] - a[0];
if(a[0]==a[i])
cnt1++;
if(a[n-1]==a[i])
cnt2++;
}
if(cnt1==n)
ans2 = (cnt1*(cnt1-1))/2;
else
ans2 = (cnt1*cnt2);
cout << ans1 << " " << ans2 << endl;
}
return 0;
}
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;
#define sc(n) scanf("%d", &n)
#define sf scanf
#define pf printf
#define MX 1000005
typedef long long ll;
ll a[MX];
int main()
{
int n, i;
ll ans1, ans2, cnt1, cnt2;
while(sc(n)==1)
{
for(i=0; i<n; i++)
scanf("%lld", &a[i]);
sort(a, a+n);
cnt1 = cnt2= 0;
for(i=0; i<n; i++)
{
ans1 = a[n-1] - a[0];
if(a[0]==a[i])
cnt1++;
if(a[n-1]==a[i])
cnt2++;
}
if(cnt1==n)
ans2 = (cnt1*(cnt1-1))/2;
else
ans2 = (cnt1*cnt2);
cout << ans1 << " " << ans2 << endl;
}
return 0;
}
460B - Little Dima and Equation (Codeforces)
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;
#define pf printf
#define MX 10005
const double eps = 1e-9;
typedef long long int ll;
ll arr[MX];
int main()
{
ll a, b, c, x;
ll temp, i, j=0, sum, temp2;
cin >>a >> b >> c;
for(i=1; i<83; i++)
{
x = i;
temp = 0;
temp = (b*pow(x, a)) + c + eps;
if(temp>1000000000)
continue;
sum = 0;
temp2 = temp;
while(temp2!=0)
{
sum += temp2%10;
temp2 /= 10;
}
if(sum==x)
arr[j++] = temp;
}
cout << j << endl;
for(int i=0; i<j ;i++)
cout << arr[i] << " ";
return 0;
}
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;
#define pf printf
#define MX 10005
const double eps = 1e-9;
typedef long long int ll;
ll arr[MX];
int main()
{
ll a, b, c, x;
ll temp, i, j=0, sum, temp2;
cin >>a >> b >> c;
for(i=1; i<83; i++)
{
x = i;
temp = 0;
temp = (b*pow(x, a)) + c + eps;
if(temp>1000000000)
continue;
sum = 0;
temp2 = temp;
while(temp2!=0)
{
sum += temp2%10;
temp2 /= 10;
}
if(sum==x)
arr[j++] = temp;
}
cout << j << endl;
for(int i=0; i<j ;i++)
cout << arr[i] << " ";
return 0;
}
Subscribe to:
Posts (Atom)
Compare equality of two string in C
#include <stdio.h> #include<string.h> int main() { char* country = "Bangladesh"; char* country2; ...
-
#include<iostream> #include<cstdio> #include<cmath> #include<cstring> #include<algorithm> using namespac...
-
#include<iostream> #include<cstdio> #include<algorithm> #include<cmath> #include<cstring> #include<ve...
-
#include<iostream> #include<cstdio> #include<cmath> #include<cstring> using namespace std; #define pf printf ...