Posts

Showing posts from April, 2016

Uva 11353 - A Different Kind of Sorting

/*     Verdict: Accepted     Time: 0.280 */ #include<bits/stdc++.h> #define limit 2000001 #define sf scanf #define pf printf using namespace std; typedef long long LL; struct my {     LL index, divisor; }; my factors[limit]; typedef bitset<limit>bsarry; bool cmp(my a, my b) {     if(a.divisor < b.divisor)     {         return true;     }     else if(a.divisor == b.divisor)     {         return a.index < b.index;     }     else     {         return false;     } } void fun_factors() {     bsarry bs;     LL i,j;     bs.set();     bs[0] = bs[1] = 0;     factors[1].index = 1;     factors[1].divisor = 1;     for(i=2; i<limit; i++)     {         if(bs[i])         {             factors[i].index=i;             factors[i].divisor = 1;             for(j=i*2; j<limit; j+=i)             {                 factors[j].index = j;                 factors[j].divisor = 1 + factors[j / i].divisor;                 bs[j] = 0;             }         }     }     sort(factors, factors+li

Uva 10780 - Again Prime? No Time.

/*     Author: Pranta Sarker     Problem: Uva 10780 Again Prime? No time     Verdict: Accepted     Time: 0.000 Given m and n. Find out the maximum power of m which can divide n! . This method is simple. Find how many prime factors are exist in n! . Then divide the power for every prime factors of n! by the power of prime factors of m. Find the minimum value between them. Look, m=150, n=10! then, 150=2^1 * 3^1 * 5^2 and 10! = 2^8 * 3^4 * 5^2 * 7^1; So, what will be the maximum power of m to can divide n! ? Definitely 1. Because, 2^1 is exist as 8 times in 2^8, 3^1 is exist as 4 times in 3^4 and 5^2 is exist as 1 times in 5^2. As 150^8 > 10! and 150^4 > 10! so, the answer will be 1, cause only power 1 of 150 can divide 10!. m will not be divided if the minimum value is 0. */ #include<iostream> #include<cstring> #include<cstdio> #include<cmath> #include<algorithm> #include<map> #include<bitset> #define limit 10050 #define sf scanf #define p

SPOJ-AMR11E - Distinct Primes

#include<iostream> #include<cstdio> #include<algorithm> #include<bitset> #include<map> #define N 3010 #define sf scanf #define pf printf using namespace std; typedef long long LL; typedef map<int,int> mpii; typedef map<LL,bool>mpbll; int prm[N], plen=0 , psz = N; bitset<N>bs; void sieve() {     LL i,j;     bs.set();     bs[0] = bs[1] = 0;     for(i=2; i<N; i++)     {         if(bs[i])         {             prm[plen++] = i;             for(j=i*i;j<N;j+=i)             {                 bs[j] = 0;             }         }     }     //for(i=0;i<100;i++)cout<<prm[i] << " "; } bool distict_prime(LL n) {     int i, cnt=0;     for(i=0;i<plen and prm[i] * prm[i] <= n;i++)     {         if(!(n%prm[i]))         {             while(!(n%prm[i]))             {                 n/=prm[i];             }             cnt++;         }     }     if(n > 1)     {         cnt++;     }     if(cnt >= 3)     {         retu

SPOJ-CMG - Collecting Mango

#include<iostream> #include<cstdio> #include<cstring> #include<vector> #include<queue> #include<stack> #include<map> #include<cmath> #include<algorithm> #define psb push_back #define ppb pop_back #define all(x) x.begin(),x.end() #define N 1000100 #define sf scanf #define pf printf using namespace std; typedef long long LL; typedef vector<LL>vll; LL ar[N] , num[N]; int main() {     int t , tc=0;     //cin >> t;     sf("%d",&t);     while(t--)     {         LL n, x, arlen=-1, nmlen=-1 , i , maxi=-1 , zar;         char c;         //cin >> n;         sf("%lld",&n);         //cout << "Case " << ++tc << ":\n";         pf("Case %d:\n",++tc);         getchar();         while(n--)         {             //cin >> c;             sf("%c",&c);             if(c == 'A')             {                 //cin >> x;           

Uva 10611 - The Playboy Chimp

/*     Verdict : Accepted     Time: 0.000 */ #include<bits/stdc++.h> #define sf scanf #define pf printf #define N 1000100 using namespace std; typedef long long LL; LL ar[N]; LL lowerBound(LL itm, LL sz) {     LL lo=1, 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; } LL upperBound(LL itm, LL sz) {     LL lo=1, hi=sz, 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() {     int n , i;     sf("%d",&n);     for(i=1;i<=n;i++)     {

Uva 547 - DDF

/* Solution Method: DP Verdict : Accepted Time: 0.040 */ // 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 freader

Uva 484 - The Department of Redundancy Department

#include<bits/stdc++.h> #define mx4 1000100 using namespace std; typedef long long LL; typedef map<string,int>mpsii; typedef map<string,bool>mpsb; typedef map<LL,bool>mpbll; typedef map<LL,LL>mpll; LL ar[mx4]; int main() {     LL n, len=0;     mpll mp;     while(cin >> n)     {         if(!mp.count(n)) // check existence. Return boolean data         {             ar[len++] = n;             mp[n] = 1;         }         else         {             mp[n]++;         }     }     for(LL i=0;i<len;i++)     {         cout << ar[i] << " " << mp[ar[i]] << "\n";     }     return 0; }

Uva 12895 - Armstrong Number

/* One kind of very easy Number theory problem i have ever seen !!! To find Armstrong Numbers just do summation among digits where all digits of a number is exponent by numbers of digits. That means,  Summation = 1st_number ^ total_digit + 2nd_number ^ total_digit + 3rd_number ^ total_digit + ...... + nth_numbers ^ total_digit. If the Summation is equal to the  main Number then, the number is called Armstrong Number. Let consider Number = 153, then the total_digit = 3; Summation = 1^3 + 5^3 + 3^3 = 153; so, Summation=Number, Hence the Number is an Armstrong Number. */ // Verdict : Accepted // Time :: 0.000 #include<bits/stdc++.h> #define sf scanf #define pf printf using namespace std; typedef long long LL; bool isArmstrong(LL n) {     int digit=0;     LL tmp = n, to_check=n;     while(n)     {         n/=10;         digit++;     }     LL sum=0,mul=1;     int mdo;     while(tmp)     {         mdo = tmp%10;         mul=1;         for(int i=

Uva 10324 - Zeros and Ones

/* Very Easy Brute force Problem. Just take a string of C++ and for each query check whether all characters are same from minimum query to maximum query. If not store in a vector as No otherwise store as Yes. At last print that loop. Be careful about the empty string.. */ #include<bits/stdc++.h> using namespace std; int main() {     string s;     vector<string>vs;     int tc=0;     while(getline(cin,s))     {         if(s.empty())break;         //cout << s << "\n";         vs.clear();         int n, i,j;         cin >> n;         while(n--)         {             cin >> i >> j;             int mn = min(i,j) , mx=max(i,j);             char ch = s[mn];             bool f=false;             for(int i=mn+1; i<=mx; i++)             {                 if(ch != s[i])                 {                     f=true;                     break;                 }                 ch = s[i];             }             if(f)             {   

Uva 386 - Perfect Cubes

/* Solving Method: Use simple Brute force system. Look, Time 2 sec and your complexity will be 196(200-4) * 198(200-2) * 197(200-3) * 196(200-4) which is less than 10^12 for 2 sec Time limit :) */ #include<bits/stdc++.h> #define N 50000 #define sf scanf #define pf printf using namespace std; typedef long long LL; void solution() {     LL i,j,k,l,a,b,c,d,sum;     for(i=6; i<=200; i++)     {         a = i*i*i;         for(j=2; j<=200; j++)         {             b = j*j*j;             for(k = j+1; k<=200; k++)             {                 c = k*k*k;                 for(l=k+1; l<=200; l++)                 {                     d = l*l*l;                     sum = b+c+d;                     if(sum == a)                     {                         pf("Cube = %lld, Triple = (%lld,%lld,%lld)\n",i,j,k,l);                     }                 }             }         }     } } int main() {     solution();     return 0; }

Uva 11991 - Easy Problem from Rujia Liu?

/* The Problem is vary impressive. Find the number's index among such queries. But that does not matter. The matter is Time limit 1sec. If you do linear search than your complexity may be O(N^2) or more, so, that will be inefficient for that time. Another Process, You may do Binary Search which has complexity O(lgN), but we know that the precondition of binary search is to Sort data first. That means it will take O(NlgN) complexity. So, it will also inefficient for that time. I have a method to solve that problem by using Precalculation method or DP. Uva 11991 - Easy Problem from Rujia Liu? Verdict : Accepted Time: 0.900 Solution Method: Structure, DP */ #include<bits/stdc++.h> #define N 1000010 #define sf scanf #define pf printf using namespace std; typedef long long LL; typedef map<long long, long long> mpll; typedef map<long long, bool>mpbll; struct my {     LL dx, num, cnt; }; my starr[N]; LL numarr[N], parr[N], plen=0 , starrlen=1; void input_n(LL k) {     m

Uva 11342 - Three-square

#include<bits/stdc++.h> #define N 50800 #define sf scanf #define pf printf using namespace std; typedef long long LL; struct my {     LL s, a, b; }; my ar[N]; int len=0; void allsquare() {     int i,j;     for(i=0; i<=224; i++)     {         for(j=i; j<=224; j++)         {             ar[len].a = i;             ar[len].b = j;             ar[len++].s = (i*i) + (j*j);         }     }     //for(i=0;i<100;i++)cout << ar[i].a << " " << ar[i].b << "-> " << ar[i].s << "; "; } bool isPerfectSquare(LL n) {     int qrt = sqrt(n);     if(qrt * qrt == n)     {         return true;     }     else     {         return false;     } } void solution(LL k) {     LL i , df;     int ans[4];     memset(ans, 0 , sizeof(ans));     LL c;     bool f=false;     for(i=0; i<len; i++)     {         df = k - ar[i].s; //cout << ar[i].s << " ";             if(isPerfectSquare(df))             {                

Uva 1180 - Perfect Numbers

Details about Perfect Numbers // Accepted // 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 cp(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 freader freopen("input.txt","r",stdin) // End.

Uva 11470 - Square Sums

#include<bits/stdc++.h> using namespace std; int ar[11][11],r[25]; int main() {     int n,tc=0;     while(cin >> n)     {         if(!n)         {             break;         }         int i,j,rlen=0;         for(i=0;i<n;i++)         {             for(j=0;j<n;j++)             {                 cin >> ar[i][j];             }         }         int up=0,right=0,left=0,bottom=0,p=n-1,t=n,dv,d=n-t;         if(n&1)         {             dv=(n/2)+1;         }         else         {             dv=n/2;         }         while(d != dv)         {             for(i=0;i<n;i++)             {                 up+=ar[d][i];                 ar[d][i]=0;                 left+=ar[i][d];                 ar[i][d]=0;                 right+=ar[i][p];                 ar[i][p] =0;                 left+=ar[p][i];                 ar[p][i]=0;             }             r[rlen++]=up+right+left+bottom;             up=0;right=0;left=0;bottom=0;             t--;             d=n-t;        

Uva 11588 - Image Coding

#include<bits/stdc++.h> using namespace std; typedef map<char,int>mpci; typedef long long LL; typedef vector<int>vi; int ar[30]; int main() {     int t,tc=0;     cin >> t;     while(t--)     {         memset(ar,0,sizeof(ar));         int r,c,m,n;         cin >> r >> c >> m >> n;         int rocl=r*c;         char ch;         for(int i=0;i<r;i++)         {             for(int j=0;j<c;j++)             {                 cin >> ch;                 ar[ch-'A']++;             }         }         int mx=-1, cnt=0;         for(int i=0; i<26; i++)         {             if(ar[i]>mx)             {                 mx = ar[i];                 cnt=1;             }             else if(mx==ar[i])             {                 ++cnt;             }         }         int due = rocl - (mx*cnt);         LL res = (mx*m*cnt) + (due*n);         cout << "Case " << ++tc << ": ";         cout <<

Uva 967 - Circular

#include<bits/stdc++.h> using namespace std; #define mx4 1000100 long long circular[]={2,3,5,7,11,13,17,31,37,71,73,79,97,113,131,197,199,311,337,373,719,733, 919, 971, 991, 1193, 1931, 3119, 3779, 7793,7937, 9311,9377, 11939, 19391,19937,37199,39119, 71993,91193,93719,93911,99371,193939,199933,319993,331999,391939, 393919,919393,933199,939193,939391,993319,999331}; int main() {     long long a,b;     //cout << sizeof(circular);     while(cin >> a)     {         if(a<0)         {             break;         }         cin >> b;         int cnt=0;         for(int i=0;i<55;i++)         {             if(circular[i] > b)             {                 break;             }             if(circular[i]>=a and circular[i]<=b)             {                 cnt++;             }         }         if(cnt)         {             if(cnt==1)             {                 cout << cnt << " Circular Prime.\n";             }             else