Posts

Showing posts from May, 2016

UVa 263 - Number Chains

// Accepted ==================== Approach : 1 ==========================  #include<bits/stdc++.h> using namespace std; typedef long long LL; typedef vector<LL>vll; typedef map<LL, bool> mpbll; vll v1 , v2; void get_num(LL n) {     v1.clear();     v2.clear();     while(n)     {         v1.push_back(n % 10);         v2.push_back(n % 10);         n/=10;     }     //for(int i=0; i<v1.size(); i++) cout << v1[i] << " "; } LL one() {     LL r=0;     sort(v1.begin() , v1.end());     int len=v1.size();     for(int i=0; i<len; i++)     {         r = v1[i] + (r * 10);     }     return r; } LL two() {     LL r=0;     sort(v2.rbegin() , v2.rend());     int len = v2.size();     for(int i=0; i<len; i++)     {         r = v2[i] + ( r * 10);     }     return r; } int main() {     ios_base::sync_with_stdio(0);     LL n;     mpbll mp;     while(cin >> n and n)     {         mp.clear(); //        get_num(n); //        LL a = one(); //cout <<

UVa 10100 - Longest Match

// Accepted #include<bits/stdc++.h> #define high 10010 #define up 1 #define down -1 #define diag 0 using namespace std; typedef vector<string>vs; vs x,y; string s1, s2; int c[high][high] , b[high][high]; void LCS(int m, int n) {     int i,j;     for(i=1; i<=m; i++)     {         for(j=1; j<=n; j++)         {             if(x[i-1] == y[j-1])             {                 c[i][j] = c[i-1][j-1]+1;                 b[i][j] = diag;             }             else if(c[i-1][j] >= c[i][j-1])             {                 c[i][j] = c[i-1][j];                 b[i][j] = up;             }             else             {                 c[i][j] = c[i][j-1];                 b[i][j] = down;             }         }     }     cout << "Length of longest match: " << c[m][n] << "\n"; } void convert(bool f) {     string tmp;     int i , len;     if(f)     {         len=s1.length();         for(i=0; i<=len; i++)         {             if((s1[i]>

LightOj 1110 - An Easy LCS

// 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> #define to_fast ios_base::sync_with_stdio(0) #define up 1 #define down -1 #define diag 0 using namespace std; int b[105][105] , c[105][105] , chlen=0; char ch[105]; string x, y , pre[105][105]; int LCS(int m , int n) {     int i , j;     for(i=1; i<=m; i++)     {         c[i][0]=0;     }     for(j=0; j<=n; j++)     {         c[0][j]=0;     }     for(i=1; i<=m; i++)     {         for(j=1; j<=n; j++)         {             if(x[i-1] == y[j-1])             {                 c[i][j] = c[i-1][j-1] + 1;                 b[i][j] = diag;                 pre[i][j] = pre[i-1][j-1] +

UVa 497 - Strategic Defense Initiative

// Accepted #include<cstdio> #include<iostream> #include<cmath> #include<cstring> #include<string> #include<sstream> #define high 10110 using namespace std; typedef long long LL; const LL inf = 2147483648; LL seq[high], I[high] , L[high] , lisseq[high]; void LIS(int n) {     int i , j;     LL len=0;     I[0] = -inf;     for(i=1; i<=n; i++)     {         I[i] = inf;     }     for(i=0; i<n; i++)     {         L[i] = 1;     }     for(i=0; i<n; i++)     {         int lo=0, hi=len, mid;         while(lo <= hi)         {             mid = (lo + hi) / 2;             if(I[mid] < seq[i])             {                 lo = mid + 1;             }             else             {                 hi = mid - 1 ;             }         }         I[lo] = seq[i];         L[i] = lo;         if(len < lo)         {             len = lo;         }     }     cout << "Max hits: " << len <<"\n" ;     i=0;     for(j=1;

UVa 10534 - Wavio Sequence

// Accepted #include<iostream> #include<cstdio> #include<cmath> #include<algorithm> #define sf scanf #define pf printf #define high 10010 using namespace std; typedef long long LL; const LL inf = 2147483648; int n; LL seq[high] , I[high], L1[high] , L2[high]; void LIS() {     int i, j;     LL len=0;     I[0] = -inf;     for(i=1; i<=n; i++)     {         I[i] = inf;     }     for(i=0; i<n; i++)     {         int lo=0, hi=len, mid;         while(lo <= hi)         {             mid = (lo + hi) / 2;             if(I[mid] < seq[i])             {                 lo = mid+1;             }             else             {                 hi = mid - 1;             }         }         I[lo] = seq[i];         L1[i] = lo;         if(len < lo)         {             len = lo;         }     } } void LDS() {     int i,j;     LL len = 0;     I[0] = -inf;     for(i=1; i<=n; i++)     {         I[i] = inf;     }     for(i=n-1; i>=0; i--)     {         int lo=0

UVa 10282 - Babelfish

#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<map> #include<string> #include<sstream> #define sf scanf #define pf printf #define high 100100 using namespace std; typedef map<string , string>mpss; mpss mp; void storage(string x) {     int i=0 , len=x.length();     string first="" , second="";     while(x[i] != ' ')     {         first+=x[i];         i++;     }     while(i < len)     {         if(x[i] != ' ')         {             second+=x[i];         }         i++;     }     mp[second] = first; } int main() {     string tmp;     while(getline(cin , tmp))     {         if(tmp.length() == 0) break;         storage(tmp);     }     string s;     while(cin >> s)     {         if(!mp[s].size())         {             cout << "eh\n";         }         else         {             cout << mp[s] << "\n";      

UVa 350 - Pseudo-Random Numbers

// Accepted #include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<map> #define sf scanf #define pf printf using namespace std; typedef map<int , bool>mpbi; int main() {     int z, i, m, l , ans=0 , tc=0;     mpbi mp;     while(~sf("%d %d %d %d" , &z, &i, &m, &l))     {         mp.clear();         if(!z and !i and !m and !l) break;         int cnt=0;         ans = ((z * l) + i) % m;         while(!mp[ans])         {             cnt++;             mp[ans] = true;             ans = ((z * ans) + i) % m;         }         pf("Case %d: %d\n", ++tc, cnt);     }     return 0; }

UVa 481 - What Goes Up

// Accepted #include<iostream> #include<cstdio> #include<cmath> #include<cstring> #include<vector> #include<algorithm> #include<map> #define sf scanf #define pf printf #define mx4 1000100 using namespace std; typedef long long LL; LL seq[mx4], lisseq[mx4], L[mx4], I[mx4]; const long long inf = 2147483648; LL Binary_Search(LL lo, LL hi, LL itm) {     LL mid;     while(lo <= hi)     {         mid = (lo + hi) / 2;         if(I[mid] < itm)         {             lo = mid+1;         }         else         {             hi = mid-1;         }     }     return lo; } LL LIS(int n) {     int i , Llen=0;     LL r , len=0 ;     for(i=0; i<n; i++)     {         r = Binary_Search(0, len, seq[i]);         I[r] = seq[i];         L[Llen++] = r;         if(len < r)         {             len = r;         }     } //cout << Llen;     return len; } void find_sequence(LL mxlen , int n) {     int i = 0, j;     for(j=1; j<n; j++)     {         if(L

Uva 11621 - Small Factors

// Accepted #include<bits/stdc++.h> #define high 2147483648 #define to_fast ios_base::sync_with_stdio(0) #define sf scanf #define pf printf using namespace std; typedef long long LL; typedef vector<LL>vll; typedef set<LL>sell; vll two, three, ans; sell s; int anslen; void storage(int n, int p) {     LL res=1;     for(int i=1; i<=p; i++)     {         if(res > high)         {             return;         }         else         {             res *= n;         }     }     s.insert(res);     if(n == 2)     {         two.push_back(res);     }     else     {         three.push_back(res);     } } void small_factors() {     for(int i=1; i<32; i++)     {         storage(2 , i);         storage(3 , i);     }     int twolen = two.size() , threelen = three.size();     //cout << twolen << " " << threelen;     LL man;     for(int i=0; i<twolen; i++)     {         for(int j=0; j<threelen; j++)         {             man = two[i] * three[j] ;

Uva 11752 - The Super Powers

// Accepted #include<iostream> #include<cstdio> #include<cmath> #include<cstring> #include<vector> #include<bitset> #include<set> #define limit 90 #define high 18446744073709551615 using namespace std; typedef unsigned long long ull; typedef long long LL; typedef vector<LL>vll; typedef set<ull>sull; bool flag[limit+5]; void sieve() {     memset(flag, false, sizeof(flag));     int i,j;     for(i=4; i<=limit; i+=2)     {         flag[i] = true;     }     for(i=3 ; i*i<=limit; i+=2)     {         if(!flag[i])         {             for(j=i*i;j<=limit;j+=(2*i))             {                 //setbit(j);                 flag[j] = true;             }         }     } //    for(i=0;i<101;i++) //    { //        if(flag[i]) //        { //            cout << i << "; "; //        } //    } } ull mpower(ull n, int p) {     ull ret = 1;     for(int i=1; i<=p; i++)     {         if(ret > (high / n))        

LightOj 1220 - Mysterious Bacteria

#include<cstdio> #include<cstring> #include<cmath> #include<vector> #define sf scanf #define pf printf #define limit 1000005 #define psb push_back using namespace std; typedef long long LL; int prime[(limit >> 6) + 1] , plen; #define setbit(n) (prime[n>>6] |= (1 << ((n>>1) & 31))) #define checkbit(n) (prime[n>>6] & (1 << ((n>>1) & 31))) vector<LL>prm; void sieve() {     LL i,j;     for(i=3; i*i<=limit; i+=2)     {         if(!checkbit(i))         {             for(j=i*i;j<=limit;j+=i+i)             {                 setbit(j);             }         }     }     prm.psb(2);     for(i=3; i<=limit; i+=2)     {         if(!checkbit(i))         {             prm.psb(i);         }     }     plen = prm.size(); } int factorize(LL n) {     int count_factor=0, mini=100000;     for(int i=0;i<plen and prm[i]*prm[i]<=n; i++)     {         if(!(n%prm[i]))         {             count_factor=0;          

Uva 10622 - Perfect P-th Powers

/*     Verdict: Accepted     1st Approach */ #include<bits/stdc++.h> #define limit 1000005 #define psb push_back #define sf scanf #define pf printf using namespace std; template<class T> T gcd(T a, T b) {return b!=0 ? gcd<T>(b,a%b) : a;} typedef long long LL; typedef unsigned long long ull; typedef vector<LL>vll; int prime[(limit >> 6) + 1]; LL prm[(limit>>1) + 9], plen=0; #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<=limit; i+=2)     {         if(!checkbit(i))         {             for(j=i*i;j<=limit;j+=i+i)             {                 setbit(j);             }         }     }     prm[plen++] = 2;     for(i=3; i<=limit; i+=2)     {         if(!checkbit(i))         {             prm[plen++] = i;         }     } } int factorize(LL n) {     int count_factor=0, mini=100005;     for(int

Uva 1644 - Prime Gap

// Verdict: Accepted #include<iostream> #include<cstdio> #include<cmath> #include<cstring> #include<map> #include<vector> #include<bitset> using namespace std; #define sf scanf #define pf printf #define sz 1299711 #define lmt 100005 typedef long long LL; typedef vector<LL>vll; LL prm[sz],plen=1; int ans[sz]; bitset<sz>bs; void sieve() {     LL i , j , pre=2, now=2, anslen=2;     bs.set();     bs[0] = bs[1] = 0;     for(i=2; i<=sz ; i++)     {         if(bs[i])         {             prm[plen++] = i;             now = i;             ans[i] = 0;             if((now - pre) != 1)             {                 LL d = now - pre;                 for(LL k=pre+1; k<now; k++)                 {                     ans[k] = d;                 }             }             pre = i;             now = 0;             for(j=i*i; j<=sz ; j+=i)             {                 bs[j] = 0;             }         }     }     //for(i=1;i<=100;i++)co

Uva 11192 - Group Reverse

#include<bits/stdc++.h> using namespace std; int main() {     ios_base::sync_with_stdio(0);     int g;     string s , res;     while(cin >> g)     {         if(!g)break;         cin.ignore();         res = "";         cin >> s;         int len = s.size();         int dvds = len / g;         while(!s.empty())         {             reverse(s.begin(),s.begin()+dvds);             //cout << s << " ";             len = s.size();             for(int i=0;i<len;i++)             {                 if(i < dvds)                 {                     res+=s[i];                 }                 else                 {                     break;                 }             }             s.erase(0,dvds);         }         cout << res << "\n";     }     return 0; }

Uva 1210 - Sum of Consecutive Prime Numbers

/* Do Cumulative Sum among Prime Numbers till 10000. Count the Sum and Print it with Constant Complexity :) */ /*     Verdict : Accepted */ #include<iostream> #include<cstdio> #include<cmath> #include<algorithm> #include<vector> #include<map> #include<cstring> #include<bitset> #define N 10005 #define psize 1300 #define sf scanf #define pf printf using namespace std; typedef long long LL; typedef vector<LL>vll; bitset<N>bs; LL prm[psize],plen=1; int ans[N]; void sieve() {     LL i,j;     bs.set();     bs[0] = bs[1] = 0;     LL sum=0;     for(i=2; i<N; i++)     {         if(bs[i])         {             prm[plen++] = i;             ans[i]+=1;             for(j=i*i;j<N;j+=i)             {                 bs[j] = 0;             }         }     }     for(i=2; i<plen; i++)     {         sum = prm[i-1];         for(j=i; j<=plen; j++)         {             sum += prm[j];             if(sum <= N)             {     

Uva 10650 - Determinate Prime

/*     Verdict : Accepted     Link: Determinate Prime Solution Link */ #include<bits/stdc++.h> #define limit 32009 #define sf scanf #define pf printf using namespace std; typedef long long LL; typedef vector<int>vi; int prm[limit], plen=1; bitset<limit> bs; void sieve() {     LL i,j;     bs.set();     bs[1] = bs[0] = 0;     for(i=2; i<limit; i++)     {         if(bs[i])         {             if(i != 2)             {                 prm[plen++] = i;             }             for(j=i*i;j<limit;j+=i)             {                 bs[j] = 0;             }         }     } } struct dprime {     int dwn, up, dist; }; dprime ar[limit]; void ek() {     int i,pd;     for(i=1; i<plen; i++)     {         pd = prm[i+1] - prm[i];         ar[i].dwn = prm[i];         ar[i].up = prm[i+1];         ar[i].dist = pd;     }     //for(i=1; i<=10; i++)cout << ar[i].dwn << " " << ar[i].u

LightOj 1088 - Points in Segments

#include<iostream> #include<cstdio> #include<cmath> #include<cstring> #include<stack> #include<queue> #include<algorithm> #include<vector> #include<map> #include<bitset> #define N 100100 #define Q 50050 #define sf scanf #define pf printf using namespace std; typedef long long LL; LL ar[N]; int lowerBound(LL itm , int sz) {     int lo=0, hi=sz, mid;     while(lo <= hi)     {         mid = (lo + hi) / 2;         if(ar[mid] == itm) hi=mid-1;         else if(ar[mid] > itm) hi = mid-1;         else if(ar[mid] < itm) lo = mid+1;     }     return lo; } int uperBound(LL itm, int sz) {     int lo=0, hi=sz, mid;     while(lo <= hi)     {         mid = (lo + hi) / 2;         if(ar[mid] == itm) lo=mid+1;         else if(ar[mid] < itm) lo=mid+1;         else if(ar[mid] > itm) hi=mid-1;     }     return lo; } int main() {     int t , tc=0;     sf("%d",&t);     while(t--)     {         int i,n,p;         sf(&

Codeforces Almost Prime

/*     Verdict: Accepted */ #include<iostream> #include<cstdio> #include<cmath> #include<bitset> #define sf scanf #define pf printf #define limit 3005 using namespace std; int prm[limit], plen=0; int ans[limit]; typedef long long LL; typedef bitset<limit>bs; bs btst; void sieve() {     LL i,j;     btst.set();     btst[0] = btst[1] = 0;     for(i=2; i<limit; i++)     {         if(btst[i])         {             for(j=i*2; j<limit; j+=i)             {                 if(!(j % i))                 {                     ans[j]+=1; // Count which number has 2 Distinct Prime factors                 }                 btst[j] = 0;             }         }     }     //for(i=2; i<=30; i++)cout<<i<<"-"<<ans[i]<<"; ";     for(i=2; i<limit; i++)     {         if(ans[i] == 2)         {             ans[i] = 1; // overlapped by 1 where got 2.         }         else         {             ans[i] = 0; // otherwise 0