Posts

Showing posts from July, 2016

Codeforces Alyona and Numbers

// Accepted #include<bits/stdc++.h> using namespace std; #define tofast ios_base::sync_with_stdio(false) #define high 100000010 typedef long long LL; int main() {     tofast;     LL n, m , i , mul=5 , d, due_col=0 , cnt=0 , quotient;     while(cin >> n >> m)     {         LL i, cnt=0, quotient=0, d=0, due_col=0 , mul=5;         for(i=1; i<=n; i++)         {             if(mul <= i)             {                 quotient = i / mul;                 mul = (quotient * 5) + 5;             }             if(mul > i)             {                 d = mul - i;                 if(d <= m)                 {                     cnt+=1;                     due_col = m - d;                     cnt+=(due_col / 5); // proti 5 ghor ontor ekta pair ache ze pair er summation 5 dara divisible.                 }             }             mul = 5;         }         cout << cnt << "\n";     }     return 0; }

Codeforces Beautiful Year

#include<bits/stdc++.h> using namespace std; typedef long long LL; int ar[11]; bool isyear(int n) {     memset(ar, 0, sizeof(ar));     while(n)     {         ar[n%10]++;         if(ar[n%10] > 1) return false;         n/=10;     }     return true; } int main() {     ios_base::sync_with_stdio(false);     int n;     while(cin >> n)     {         int x,y,z;         for(int i=n+1; ; i++)         {             if(isyear(i))             {                 cout << i << "\n";                 break;             }         }     }     return 0; }

Codeforces Nearly Lucky Number

#include<bits/stdc++.h> using namespace std; typedef long long LL; int main() {     ios_base::sync_with_stdio(false);     LL n;     while(cin >> n)     {         LL cnt=0;         while(n)         {             if(n%10 == 4 or n%10==7) cnt++;             n/=10;         }         if(cnt==4 or cnt==7 or cnt==47 or cnt==744) cout << "YES\n";         else cout << "NO\n";     }     return 0; }

Codeforces Extra-terrestrial Intelligence

#include<bits/stdc++.h> using namespace std; int dis[110]; int main() {     ios_base::sync_with_stdio(false);     freopen("input.txt","r",stdin);     freopen("output.txt","w",stdout);     int n;     while(cin >> n)     {         int d , keep=0 , len=0;         char x;         bool f=false , extra=false;         for(int i=0; i<n; i++)         {             cin >> x;             if(x == '1')             {                 if(!f)                 {                     dis[i] = i;                     keep = i;                     f=true;                 }                 else                 {                     keep = i - keep;                     len++;                     dis[len] = keep;                     keep = i;                     if(len > 1)                     {                         if(dis[len] != dis[len-1]) extra=true;                     }                 }             }         }         if(extra) co

Codeforces Lovely Palindromes

#include<bits/stdc++.h> using namespace std; typedef long long LL; typedef unsigned long long ull; int main() {     ios_base::sync_with_stdio(false);     string s;     while(cin >> s)     {         cout << s;         for(LL i=s.length()-1; i>=0; i--) cout << s[i];         //cout << s << "\n";         cout << "\n";     }     return 0; }

Hacker Rank Euler's Criterion

#include<iostream> #include<cstdio> #include<cmath> #include<cstring> #define sf scanf #define pf printf #define limit 32000 using namespace std; typedef long long LL; LL bigmod(LL a, LL b , LL c) {     if(!b) return 1;     if(!(b&1))     {         LL r = bigmod(a, b/2, c);         return ((r%c) * (r%c))%c;     }     else     {         return ((a%c) * (bigmod(a, b-1, c)%c))%c;     } } bool isQuadratic_residue(LL n, LL p) {     if(bigmod(n, (p-1)/2 , p) == 1) return true;     else return false; } int main() {     int t;     sf("%d", &t);     while(t--)     {         LL a,m;         sf("%lld %lld", &a, &m);         if(isQuadratic_residue(a,m) or a==0) pf("YES\n");         else pf("NO\n");     }     return 0; }

Codeforces Game of Robots

// Accepted #include<bits/stdc++.h> using namespace std; #define fast ios_base::sync_with_stdio(false) #define high 1000010 typedef long long LL; struct mystruct {     LL seqval, realval; }; mystruct ar[high]; LL khoj(LL lo, LL hi, LL itm) {     LL mid;     while(lo <= hi)     {         mid = (lo + hi) / 2;         if(ar[mid].seqval == itm) return mid;         else if(ar[mid].seqval > itm) hi=mid-1;         else if(ar[mid].seqval < itm) lo=mid+1;     }     return lo; } int main() {     fast;     LL n,k,i;     while(cin >> n >> k)     {         LL x , tmp=0;         for(i=1; i<=n; i++)         {             cin >> x;             tmp+=i;             ar[i].realval = x;             ar[i].seqval = tmp;         }         //for(i=1; i<=n; i++)cout << ar[i].realval << " " << ar[i].seqval << "\n";         LL indx = khoj(1, n, k); //cout << indx << " " << ar[indx].seqval << "

Codeforces Holidays

// Accepted // reminder 6 er jonno minimum 1 hobe karon 5 din working day er por 6 number day off day, basically maximum 2 kore agabe, // but except reminder 6. :) #include<bits/stdc++.h> using namespace std; #define fast ios_base::sync_with_stdio(false) typedef long long LL; int main() {     fast;     LL n;     while(cin >> n)     {         LL qotient = n / 7;         LL reminder = n % 7;         LL mx=(qotient*2), mn=mx;         if(reminder > 0)         {             if(reminder == 1)             {                 mx+=1;             }             else             {                 mx+=2;             }             if(reminder == 6)             {                 mn+=1;             }         }         cout << mn << " " << mx << "\n";     }     return 0; }

UVa 10692 - Huge Mods

// Accepted // Time: 0.00 // Theory: a ^ ( b % phi[m]) + phi[m] // suppose, m=53, n=3, and a1=2, a2=3 and a3=2; then: m=53, m1=phi[m]=52, m2=phi[m1]=24; // we know: 2 ^ 3 ^ 2 % m ; that means it will be 2 ^ 3 ^ 2 ^ 1 // We have to start from the last, so: ans1 = (2 ^ 1 % m2) + m2=26; which is mod(2 , ans, m2), ans=1 here; // ans2 = (( 3 ^ ans1) % m1) + m1 = 61; which is mod(3 , ans, m1), ans=ans1; // main_ans = (2 ^ ans2) % m=35; which is mod(2 , ans, m), ans = ans2; // Good Day... :) Happy Coding #include<bits/stdc++.h> using namespace std; #define high 10010 #define sf scanf #define pf printf char ch[high]; int phi[high] , ar[high], br[high]; void Euler_phi() {     int i,j;     phi[1] = 1;     for(i=2; i<high; i++)     {         if(!phi[i])         {             phi[i] = i-1;             for(j=2*i; j<high; j+=i)             {                 if(!phi[j])                 {                     phi[j] = j;                 }                 phi[j]/=i;                 phi[j]*=

Codeforces Cells Not Under Attack

#include<iostream> #include<cstdio> #include<cmath> #include<cstring> #include<algorithm> #include<map> #define cf %I64d using namespace std; #define sf scanf #define pf printf typedef long long LL; typedef map<LL,bool>mpll; int main() {     LL n,m;     mpll ro, col;     while(~sf("%I64d %I64d", &n, &m))     {         ro.clear();         col.clear();         LL i , Row=0, Column=0;         for(i=0; i<m; i++)         {             LL x,y;             sf("%I64d %I64d", &x, &y);             if(!ro[x])             {                 Row++;                 ro[x]=true;             }             if(!col[y])             {                 Column++;                 col[y] = true;             }             //cout << Row << " " << Column << "\n";             LL ans = (n - Row) * (n - Column);             pf("%I64d\n", ans);         }     }     return 0; }

Codeforces Cards

#include<iostream> #include<cstdio> #include<cmath> #include<cstring> #include<algorithm> #include<map> using namespace std; #define sf scanf #define pf printf typedef map<int , bool>mpbi; int ar[110]; int main() {     int n;     mpbi mp;     while(~sf("%d", &n))     {         int i,j, sum=0;         mp.clear();         for(i=1; i<=n; i++)         {             sf("%d", &ar[i]);             sum+=ar[i];         }         sum = sum / (n / 2); //cout << sum;         int k=0;         for(i=1; i<=n; i++)         {             for(j=i+1; j<=n; j++)             {                 if(ar[i] + ar[j] == sum)                 {                     //pf("%d %d\n", i, j);                     //break;                     if(!mp[i] and !mp[j])                     {                        mp[i]=true;                        mp[j] = true;                        pf("%d %d\n", i, j);                    

SPOJ Prime Intervals

// Accepted // Time: 0.74 #include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> using namespace std; #define sf scanf #define pf printf #define DIFF 1000010 #define limit 46400 #define l_limit 216 #define mset(c,v) memset(c , v , sizeof(c)) typedef long long LL; LL prm[DIFF], plen=0; bool mark[DIFF], primenow[DIFF]; void sieve(LL n) {     LL i,j;     mset(mark, true);     for(i=2; i*i<limit; i++)     {         if(mark[i])         {             for(j=i*i; j<limit; j+=i)             {                 mark[j] = false;             }         }     }     for(i=2; i<limit; i++)     {         if(mark[i])         {             prm[plen++] = i;         }     } } int main() {     int t;     sf("%d", &t);     while(t--)     {         plen=0;         LL L, U , i, j , p , s;         sf("%lld %lld", &L, &U);         sieve(U);         mset(primenow, true);         for(i=0; i<plen; i++)       

SPOJ Prime Generator

// Accepted // Time: 0.01 // Process: Segmented Seieve #include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<algorithm> using namespace std; typedef long long LL; #define DIFF 100010 #define mset(c,v) memset(c,v,sizeof(c)) #define sf scanf #define pf printf LL prm[DIFF], plen=0; void sieve(int n) {     LL i,j;     bool mark[DIFF];     //mset(mark, true);     for(i=2; i<DIFF; i++) mark[i] = true;     LL qrt = floor(sqrt(double(n)));     LL k = floor(sqrt (double(qrt)));     for(i=2; i<=k; i++)     {         if(mark[i])         {             for(j=i*i; j<=qrt; j+=i)             {                 mark[j] = false;             }         }     }     for(i=2; i<=qrt; i++)     {         if(mark[i])         {             prm[plen++] = i;         }     }     //for(i=0; i<plen; i++) cout << prm[i] << " ";     pf("\n");

Codeforces Mike and Cellphone

#include<bits/stdc++.h> #define sf scanf #define pf printf using namespace std; int main() {     int n;     char ch[11];     while(~sf("%d", &n))     {         getchar();         int i=0;         for(i=0; i<n; i++)         {             sf("%c",&ch[i]);         }         int up=1, right=1, left=1, down=1;         for(i=0; i<n; i++)         {             int tmp = ch[i] - 48;             if(tmp==1 or tmp==2 or tmp==3) up=0;             if(tmp==1 or tmp==4 or tmp==7) left=0;             if(tmp==3 or tmp==6 or tmp==9) right=0;             if(tmp==7 or tmp==9) down=0;             if(tmp==0) down=0, left=0, right=0;         }         if(up==1 or right==1 or left==1 or down==1) cout << "NO\n";         else cout << "YES\n";     }     return 0; }

Codeforces Fashion in Berland

#include<bits/stdc++.h> using namespace std; int main() {     ios_base::sync_with_stdio(false);     int n;     while(cin >> n)     {         int cnt=0 , i=0;         while(i<n)         {             int x;             cin >> x;             if(x) cnt++;             i++;         }         if(n==1)         {             if(cnt>0) cout << "YES\n";             else cout << "NO\n";         }         else         {             if(cnt == (n-1)) cout << "YES\n";             else cout << "NO\n";         }     }     return 0; }

Codeforces Comparing Two Long Integers

#include<bits/stdc++.h> using namespace std; typedef long long LL; int main() {     ios_base::sync_with_stdio(false);     string a,b;     while(cin >> a >> b)     { //        if(a > b) cout << ">\n"; //        else if(a < b) cout << "<\n"; //        else if(a == b) cout << "=\n";         //cout << a.length() << " " << b.length() << "\n";             string tmp="";             LL dif=0 , i=0;             bool a_boro=false, b_boro=false;             if(a.length() > b.length())             {                 dif = a.length() - b.length();                 a_boro = true;             }             else if(a.length() < b.length())             {                 dif = b.length() - a.length();                 b_boro=true;             }             if(a_boro)             {                 tmp="";                 for(i=0; i<dif; i++)                

Codeforces Launch of Collider

#include<bits/stdc++.h> using namespace std; typedef long long LL; #define high 200010 #define inf 2147483647 #define sf scanf #define pf printf struct my {     char ch;     LL v; }; my ar[high]; LL br[high]; int main() {     LL n;     while(~sf("%lld", &n))     {         getchar();         LL i=0 , j=0 , x;         char dir;         for(j=1; j<=n; j++)         {             sf("%c", &dir);             i++;             ar[i].ch = dir;         }         i=0;         //getchar();         for(j=1; j<=n; j++)         {             sf("%lld", &x);             i++;             ar[i].v=x;         }         //for(i=0; i<n; i++) cout << ar[i].ch << " " << ar[i].v << "\n";         LL k=1 , cnt = inf;         for(i=1; i<=n-1; i++)         {             if(ar[i].ch == 'R' and ar[i+1].ch == 'L')             {                 cnt = min(cnt, (abs(ar[i].v - ar[i+1].v)) / 2);        

Codeforces Pineapple Incident

#include<cstdio> #include<iostream> #include<cmath> #include<algorithm> #include<vector> #include<map> #define sf scanf #define pf printf #define high 1000000000 using namespace std; typedef long long LL; int main() {     LL t, s, x;     while(~sf("%lld %lld %lld", &t, &s, &x))     {         // equation: t + (s * y) = x;         LL sub = abs(x - t);         LL y = sub / s;         LL ans = t + (s * y);         if(ans == x)         {             pf("YES\n");         }         else         {             if(y!=0)             {                 ans++;             }             if(ans == x)             {                 pf("YES\n");             }             else             {                 pf("NO\n");             }         }     }     return 0; }

UVa 11408 - Count DePrimes

// Accepted // Seieve, DP #include<bits/stdc++.h> using namespace std; #define high 5000010 #define sf scanf #define pf printf typedef long long LL; typedef vector<LL>vl; bitset<high>bs; LL ar[high]; LL prime[(high>>6) + 1]; vl prm; #define setbit(n) (prime[n>>6] |= (1 << ((n >> 1) & 31))) #define checkbit(n) (prime[n>>6] & (1 << ((n >> 1) & 31))) void sieve() {     LL i,j;     for(i=3; i*i<high; i+=2)     {         if(!checkbit(i))         {             for(j=i*i; j<high; j+=(2*i))             {                 setbit(j);             }         }     }     prm.push_back(2);     for(i=3; i<high; i+=2)     {         if(!checkbit(i))         {             prm.push_back(i);         }     }     //for(i=0; i<100; i++)cout << prm[i] << " ";     //cout << prm[prm.size()-1];     //cout << prm.size(); } void deprimes() {     LL i,j;     bs.set();     ar[2] = 2;     for(i=4; i<

SPOJ Prime Time

#include <iostream> #include <cstdio> #include <cstring> #include <map> #include <string> #include <vector> #include <cmath> #include <cctype> #include <sstream> #include <set> #include <list> #include <stack> #include <queue> #include <algorithm> #define sf scanf #define pf printf #define sfint scanf ("%d %d",&a,&b) #define sfl scanf ("%ld %ld",&a,&b) #define sfll scanf ("%lld %lld",&a,&b) #define sfd scanf ("%lf %lf",&a,&b) #define sff scanf ("%f %f",&a,&b) #define loop(i,n) for(i=0;i<n;i++) #define LL long long #define L long #define nl puts("") #define MX 10100 #define N 100 #define MOD 10000000007 #define pb push_back #define pi acos(-1.0) #define sz size() #define gc getchar () #define ps push #define clr clear #define bn begin() #define ed end() using namespace std; int prime[(MX >> 6) +

Uva 10925 - Krakovia

import java.util.*; import  java.math.*; class Main {     public static void main (String[] args)     {         Scanner in = new Scanner(System.in);                 int n , f, tc=0;                 while(in.hasNext())         {             n = in.nextInt();             f = in.nextInt();                         if(n==0 && f==0) break;                         BigInteger total = BigInteger.valueOf(0);             BigInteger v;                         for(int i=0; i<n; i++)             {                 v = in.nextBigInteger();                                 total = total.add(v);             }                         System.out.println("Bill" + " #" + (++tc) + " costs " + total + ": " + "each friend should pay " + (total.divide(BigInteger.valueOf(f))));             System.out.print("\n");         }     } }

SPOJ Finding the Kth Prime

// Header file begin #include<iostream> #include<cstdio> #include<cstring> #include<map> #include<string> #include<vector> #include<cmath> #include<cctype> #include<sstream> #include<set> #include<list> #include<stack> #include<utility> #include<queue> #include<algorithm> #include<cstdlib> #include<bitset> // End //.......... // Macro #define sf scanf #define pf printf #define lp1(i,n) for(i=0;i<n;i++) #define lp2(i,n) for(i=1;i<=n;i++) #define mset(c,v) memset(c,v,sizeof(c)) #define outs(a) cout<<" "<<a<<" " #define nl puts("") #define sq(x) ((x)*(x)) #define all(x) x.begin(),x.end() #define reall(x) x.rbegin(),x.rend() #define s_wap(x,y) x^=y;y^=x;x^=y; #define sz size() #define gc getchar() #define psb push_back #define fre

UVa 11736 - Debugging RAM

#include<bits/stdc++.h> using namespace std; typedef map<string, int>mpsi; typedef unsigned long long ull; struct my {     string x;     int byte;     ull result; }; my ar[330]; ull power(int dui, int p) {     ull r = 1;     for(int i=1; i<=p; i++)     {         r*=dui;     }     return r; } ull decimal(string s) { //    ull ret = 0; //    int len = s.length () - 1; // //    for ( int i = len; i >= 0; i-- ) //        ret += (power (2, i) * (s [len - i] - '0')); // //    return ret;       ull ret =0, k;       int len = s.length();       int p = len - 1;       for(int i=0; i<len and p>=0; i++)       {           k = (s[i] - '0') * power(2, p);           ret+=k;           p--;       }       return ret; } int main() {     ios_base::sync_with_stdio(false);     int b, v;     mpsi mp;     while(cin >> b >> v)     {         mp.clear();         string s;         int i , y, line=0, arlen=1;         for(i=0; i<v; i++)         {             ci