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;             } ...

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))             {                 cou...

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)        ...

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 %...

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++)    ...

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;       ...

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;   ...

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])      ...

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=...

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;             }    ...

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)          ...

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;         ...

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 ...

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())     ...

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;       ...

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         {        ...

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);             }     ...

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;       ...

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++)     ...