Posts

Showing posts from August, 2016

Codeforces Codeforces Round #367 (Div. 2) B. Interesting drink

// Accepted Code Link: B. Interesting drink // Mnaual //#include<stdio.h> //#include<iostream> //#include<algorithm> //#include<vector> //#include<cstring> //#include<cmath> //#include<map> #include<bits/stdc++.h> using namespace std; #define fast ios_base::sync_with_stdio(false) #define outs(x) cout << x << " " #define outn(x) cout << x << "\n" #define sf scanf #define pf printf #define high 1000010 typedef long long LL; typedef vector<LL>vll; vll ar; //upper bound LL khoj(LL lo, LL hi, LL itm) {     LL mid;     while(lo <= hi)     {         mid = (lo + hi) / 2;         if(ar[mid] == itm) lo = mid+1;         else if(ar[mid] > itm) hi = mid - 1;         else if(ar[mid] < itm) lo=mid+1;     }     return lo; } int main() {     fast;     LL n;     while(cin >> n)     {         ar.clear();         LL i=0 , x;         for(i=0; i<n; i++)         {             cin >>

Codeforces Round #369 (Div. 2) B. Chris and Magic Square

// This is all about 311 lines code // I am request you to please see other code if you are incapable to solve it Accepted Code Link: B. Chris and Magic Square //#include<stdio.h> //#include<iostream> //#include<algorithm> //#include<vector> //#include<cstring> //#include<cmath> //#include<map> #include<bits/stdc++.h> using namespace std; #define fast ios_base::sync_with_stdio(false) #define outs(x) cout << x << " " #define outn(x) cout << x << "\n" #define sf scanf #define pf printf #define high 505 typedef long long LL; LL ar[high][high]; LL d=0 , diagsum1=0, diagsum2=0; bool gotnm=false; int n , row=0, col=0; void checkWhole() {     LL rowsum=0, colsum=0, tmprow=0, tmpcol=0 , d1=0, d2=0 , ans1=0, ans2=0;     int i,j;     bool decision=false;     for(i=0; i<n; i++)     {         rowsum=0;         colsum=0;         for(j=0; j<n; j++)         {             rowsum += ar[i][j];        

Codeforces Round #188 (Div. 2) A. Even Odds

//#include<stdio.h> //#include<iostream> //#include<algorithm> //#include<vector> //#include<cstring> //#include<cmath> //#include<map> #include<bits/stdc++.h> using namespace std; #define fast ios_base::sync_with_stdio(false) #define outs(x) cout << x << " " #define outn(x) cout << x << "\n" #define sf scanf #define pf printf typedef long long LL; int main() {     //fast;     LL n, k;     while(~sf("%I64d %I64d", &n , &k))     {         LL lim = 0;         lim = n % 2 == 0 ? n / 2 : (n / 2) + 1;         if(k > lim)         {             // even             pf("%I64d\n", 2 * (k - lim)) ;         }         else         {             // odd             pf ("%I64d\n" , 2 * (k - 1) + 1);         }     }     return 0; }

Codeforces Round #355 (Div. 2) A.Vanya and Fence

#include<bits/stdc++.h> using namespace std; #define fast ios_base::sync_with_stdio(false) #define outs(x) cout << x << " " #define outn(x) cout << x << "\n" #define sf scanf #define pf printf int main() {     fast;     int n, h;     while(cin >> n >> h)     {         int sum=0;         int x;         while(n--)         {             cin >> x;             if(x > h) sum+=2;             else sum++;         }         outn(sum);     }     return 0; }

AIM Tech Round 3 (Div. 2) C.Letters Cyclic Shift

/* You can just change i mean convert each character to it's previous character until you have not found 'a' to find lexicographically minimum.If you get 'a' that means it is already lexicographically minimum, then change any a character to its previous as one time. */ //#include<stdio.h> //#include<iostream> //#include<algorithm> //#include<vector> //#include<cstring> //#include<cmath> //#include<map> #include<bits/stdc++.h> using namespace std; #define fast ios_base::sync_with_stdio(false) #define outs(x) cout << x << " " #define outn(x) cout << x << "\n" //int ar[28]; typedef long long LL; //void ascii() //{ //    for(int i=97; i<=122; i++) //    { //        ar[i] = i; //    } //} int main() {     fast;     //ascii();     string s;     while(cin >> s)     {         LL len = s.length() , i , tmp=0;         bool f=false;     

Codeforces AIM Tech Round 3 (Div. 2) A.Juicer

#include<bits/stdc++.h> using namespace std; #define fast ios_base::sync_with_stdio(false) typedef long long LL; int main() {     fast;     LL n, b, d;     while(cin >> n >> b >> d)     {         LL cnt=0 , x , sum=0 ;         while(n--)         {             cin >> x;             if(x <= b) sum+=x;             if(sum > d) cnt++, sum=0;         }         cout << cnt << "\n";     }     return 0; }

Codeforces Round 339 A.Link/Cut Tree

// Accepted. // Link:  Codeforces AC link of problem Link/Cut Tree #include<bits/stdc++.h> using namespace std; #define fast ios_base::sync_with_stdio(false) #define high 300010 typedef unsigned long long LL; //LL power(LL x, int y) //{ //    if(y == 0) return 1; // //    LL tmp = power(x, y/2); // //    if(y % 2 == 0) //    { //        return tmp * tmp; //    } // //    else //    { //        return x * tmp * tmp; //    } //} int main() {     fast; //    LL x = 923523399718980912, y = 18686596544687500000; //    string s = x > y ? "x boro" : "y boro"; //    cout << s;     LL l, r, k;     while(cin >> l >> r >> k)     {         bool f=false , yes=false;         LL gtp=0;         //cout << power(k, 5); return 0;         for(int i=0; i<64; i++)         {             //gtp = power(k, i);             gtp = powl(k, i);             if(gtp > r) break;             //cout <<

Codeforces Round 340 A. Elephant

#include<bits/stdc++.h> using namespace std; #define fast ios_base::sync_with_stdio(false) #define high 300010 typedef long long LL; int main() {     fast;     LL n;     while(cin >> n)     {         LL ans = (n%5) ? (n/5) + 1 : n/5;         cout << ans << "\n";     }     return 0; }

Codeforces Educational Round 16 B.Optimal Point on a Line

#include<bits/stdc++.h> using namespace std; #define fast ios_base::sync_with_stdio(false) #define high 300010 typedef long long LL; LL ar[high]; int main() {     fast;     LL n;     while(cin >> n)     {         LL i;         for(i=0; i<n; i++)         {             cin >> ar[i];         }         sort(ar, ar+n);         LL ans = (n&1) ? ar[n/2] : ar[n/2 - 1];         cout << ans << "\n";     }     return 0; }

Codeforces Educational Round 16 A.King Moves

#include<bits/stdc++.h> using namespace std; #define fast ios_base::sync_with_stdio(false) int ret(char ch) {     if(ch == 'a') return 1;     else if(ch == 'b') return 2;     else if(ch == 'c') return 3;     else if(ch == 'd') return 4;     else if(ch == 'e') return 5;     else if(ch == 'f') return 6;     else if(ch == 'g') return 7;     else if(ch == 'h') return 8; } int ar[9][9]; void init() {     int i,j;     for(i=1; i<=8; i++)     {         for(j=1; j<=8; j++)         {             ar[i][j] = 1;         }     } } int main() {     fast;     int n;     char c;     init();     while(cin >> c >> n)     {         int i = n;         int j = ret(c) , cnt=0; //cout << ar[i][j] << "\n"; return 0;         if(ar[i-1][j+1]==1)cnt++;         if(ar[i][j+1]==1)cnt++;         if(ar[i-1][j]==1)cnt++;         if(ar[i-1][j-1]==1)cnt++;         if(ar[i][j-1]==1)cnt++;         if(ar[i+1][j]==

Codeforces Team Olympiad

#include<bits/stdc++.h> using namespace std; typedef map<int, bool>mpbi; struct my {     int in, v; }; bool cmp(my  a, my b) {     return a.v < b.v; } my ar[5010]; //int ar[100]; int main() {     ios_base::sync_with_stdio(false);     int n;     mpbi mp;     while(cin >> n)     {         mp.clear();         int i=0 , x, quotient=0 , one=0, two=0, three=0, mini=100000 ;         for(i=0; i<n; i++)         {             cin >> x;             ar[i].in = i;             ar[i].v = x;             //ar[i] = x;             if(x == 1) one++;             else if(x == 2) two++;             else if(x == 3) three++;             mini = min(one, two);             mini = min(mini, three);         }         //sort(ar, ar+n , cmp);         if(one==0 or two==0 or three==0)         {             cout << "0\n";             continue;         }         cout << mini << "\n";         sort(ar, ar+n, cmp); //        for(i=0; i<n; i++) //       

UVa 11614 Etruscan Warriors Never Play Chess

The Accepted Code is on github Solution Link: Solution Link of UVa 11614 Etruscan Warriors Never Play Chess

Codeforces B. Barnicle

// Accepted #include<bits/stdc++.h> using namespace std; #define fast ios_base::sync_with_stdio(false) #define out(x) cout << x << "\n" typedef unsigned long long ull; string ans=""; void fun_ek(int n) {     while(n--)     {         ans+="0";     } } int main() {     fast;     string s;     while(cin >> s)     {         ans="";         if(s[0] != '0') ans+=s[0];         int b=0 , k=1;         string tmp="";         while(s[s.length()-k] != 'e')         {             tmp+=s[s.length()-k];             k++;         }         ull i=0 ;         //out(tmp);         //out(tmp.length());         k=0;         for(k=tmp.length()-1; k>=0; k--)         {             b = (tmp[k] - 48) + (b * 10);         }         //cout << "b = " << b << "\n";         //cout << s[s.length()-3] << "\n";         //cout << "len = " << s.length(

Codeforces 192 A.Funky Numbers

#include<bits/stdc++.h> using namespace std; typedef long long LL; typedef vector<LL>vll; typedef map<LL, bool>mpbl; #define high 1000000000 #define fast ios_base::sync_with_stdio(false) int len=0; vll v; mpbl mp; void funkey() {     LL i;     for(i=1; ; i++)     {         if(((i * (i+1)) / 2 ) > high)         {             //cout << i << " " << i+1;             break;         }         v.push_back(((i * (i+1)) / 2));         mp[((i * (i+1)) / 2)] = true;     }     //for(i=0; i<50; i++) cout << v[i] << " ";     len = v.size(); } int khoj(int lo, int hi, LL itm) {     int mid;     while(lo <= hi)     {         mid = (lo + hi) / 2;         if(v[mid] == itm) return mid;         else if(v[mid] > itm) hi = mid - 1;         else if(v[mid] < itm) lo = mid + 1;     }     return lo; } int main() {     fast;     funkey();     LL n;     while(cin >> n)     {         int r = khoj(0, len-1, n);         if(v[r]

Codeforces 584 A . Olesya and Rodion

#include<bits/stdc++.h> using namespace std; typedef long long LL; int main() {     ios_base::sync_with_stdio(false);     int n , t;     while(cin >> n >> t)     {         if(t==10)         {             if(n==1)             {                 cout << "-1\n";                 continue;             }             cout << "1";             for(int i=2; i<=n; i++) cout << "0";             cout << "\n";             continue;         }         for(int i=1; i<=n; i++) cout << t;         cout << "\n";     }     return 0; }

Codeforces 678 B. The Same Calendar

#include<bits/stdc++.h> using namespace std; typedef long long LL; bool isLeap(LL n) {     return (n % 400==0 or (n%4==0 and n%100!=0)); } int main() {     ios_base::sync_with_stdio(false);     LL n;     while(cin >> n)     {         LL day=0;         for(LL i=n; ; i++)         {             if(isLeap(i)) day+=366;             else day+=365;             if(day%7==0 and isLeap(n) == isLeap(i+1) and n!=i+1)             {                 cout << i+1 << "\n";                 break;             }         }     }     return 0; }

Codeforces 678 D. Iterated Linear Function

// Accepted // Formula: ( (A^n) - 1 / A - 1) * B + ( (A ^ n) * x) // You have to do Modular Multiplicative Inverse due to (A - 1) ^ -1 #include<bits/stdc++.h> using namespace std; typedef long long LL; #define mod 1000000007 LL bigmod(LL a , LL b, LL c) {     if(b==0) return 1;     if(b % 2 == 0)     {         LL r = bigmod(a, b/2 , c);         return ((r%c) * (r%c)) % c;     }     else     {         return (((a%c) * bigmod(a, b-1 , c) % c)) % c;     } } int main() {     ios_base::sync_with_stdio(false);     LL a , b , n , x;     while(cin >> a >> b >> n >> x)     {         LL tmp, t1, t2, ans;         if(a == 1)         {             LL ans = (n%mod * b%mod + x) % mod;             cout << ans << "\n";             continue;         }         tmp = bigmod(a , n, mod);         ans = ((tmp * x%mod) + b%mod * (tmp-1)%mod * bigmod(a-1, mod-2, mod)%mod)%mod;         //t1 = ( b %mod * (tmp-1)%mod * bigmod(a-1, mod-2, mod)%mod)%mod;        

Codeforces The number of positions

**********************************  Solution 1 ***************************** #include<bits/stdc++.h> using namespace std; #define tofast ios_base::sync_with_stdio(false) int main() {     tofast;     int n, a , b;     while(cin >> n >> a >> b)     {         int ans=0;         for(int i=a+1; i<=n ; i++)         {             if(n - i <= b) ans++;         }         cout << ans << "\n";     }     return 0; } ******************************Solution 2******************************************* #include<bits/stdc++.h> using namespace std; #define tofast ios_base::sync_with_stdio(false) int main() {     tofast;     int n, a, b;     while(cin >> n >> a >> b)     {         cout << min(n-a, b+1) << "\n" ; // ultimate result to see the pattern of previous solution     }     return 0; }

Codeforces Bear and Game

#include<bits/stdc++.h> using namespace std; int main() {     int n;     while(cin >> n)     {         int x , tmp=15, ans=0;         bool bore=false;         for(int i=0; i<n; i++)         {             cin >> x;             if(!bore)             {                 if(x > tmp)                 {                     bore = true;                     ans = tmp;                 }             }             tmp = x+15;         }         if(!ans)         {             if(tmp >= 90) ans=90;             else ans=tmp;         }         cout << ans << "\n";     }     return 0; }