Sunday, February 28, 2016

LightOJ - 1077 - How Many Points?

  1. /****************#####    بِسْمِ اللَّهِ الرَّحْمَنِ الرَّحِيم   #####******************
  2. __________________________________________________________________________
  3. ######################  Ya-Seen Arafat(ACWizard) #########################
  4. ######################        UAP-CSE-33B        #########################
  5. *************************************************************************/
  6. #include <bits/stdc++.h>
  7. #define M 1000003
  8. #define S 1000003
  9. #define LL long long
  10. using namespace std;
  11.  
  12. int main(){
  13.     int t, cs = 0;
  14.     LL x1, x2, y1, y2;
  15.     LL a, b;
  16.     scanf("%d"&t);
  17.     while(t--){
  18.         scanf("%lld %lld %lld %lld"&x1, &y1, &x2, &y2);
  19.         LL p, q;
  20.         if(x1 < 0 && x2 < 0)= abs(abs(x1)-abs(x2));
  21.         else if(x1 >= 0 && x2 >= 0)= abs(x1-x2);
  22.         else if(x1 < 0 && x2 >= 0)= abs(x1)+x2;
  23.         else p = x1+abs(x2);
  24.  
  25.         if(y1 < 0 && y2 < 0)= abs(abs(y1)-abs(y2));
  26.         else if(y1 >= 0 && y2 >= 0)= abs(y1-y2);
  27.         else if(y1 < 0 && y2 >= 0)= abs(y1)+y2;
  28.         else q = y1+abs(y2);
  29.  
  30.         LL ans = __gcd(p, q)+1;
  31.         printf("Case %d: %lld\n"++cs, ans);
  32.     }
  33.     return 0;
  34. }

LightOJ - 1067 - Combinations

  1. /****************#####    بِسْمِ اللَّهِ الرَّحْمَنِ الرَّحِيم   #####******************
  2. __________________________________________________________________________
  3. ######################  Ya-Seen Arafat(ACWizard) #########################
  4. ######################        UAP-CSE-33B        #########################
  5. *************************************************************************/
  6. #include <bits/stdc++.h>
  7. #define M (long long)1000003
  8. #define S 1000003
  9. #define LL long long
  10. using namespace std;
  11.  
  12. LL factorial[M+3];
  13.  
  14. void findFact(){
  15.     factorial[0] = 1;
  16.     for(int i = 1; i < S; i++){
  17.         factorial[i] = factorial[i-1] * i;
  18.         factorial[i] %= M;
  19.     }
  20. }
  21.  
  22. LL modInverse(LL a, LL p){
  23.     if(== 0)return 1LL;
  24.     else if(p%2)return ((a%M)*(modInverse(a, p-1)%M))%M;
  25.     else{
  26.         LL x = modInverse(a, p/2);
  27.         return (x%M*x%M)%M;
  28.     }
  29. }
  30.  
  31. int main(){
  32.     memset(factorial, 1LL, sizeof(factorial));
  33.     findFact();
  34.     int t, cs = 0;
  35.     LL n, k;
  36.     scanf("%d"&t);
  37.     while(t--){
  38.         scanf("%d %d"&n, &k);
  39.         LL y = factorial[n];
  40.         LL z = (factorial[n-k]%M*factorial[k]%M)%M;
  41.         LL ans = modInverse(z, M-2LL);
  42.         ans = (y%M*ans%M)%M;
  43.         printf("Case %d: %lld\n"++cs, ans);
  44.     }
  45.     return 0;
  46. }

LightOJ - 1045 - Digits of Factorial

  1. /****************#####    بِسْمِ اللَّهِ الرَّحْمَنِ الرَّحِيم   #####******************
  2. __________________________________________________________________________
  3. ######################  Ya-Seen Arafat(ACWizard) #########################
  4. ######################        UAP-CSE-33B        #########################
  5. *************************************************************************/
  6. #include <bits/stdc++.h>
  7. #define ll long long
  8. #define S 1000003
  9. using namespace std;
  10.  
  11. double cuS[S];
  12.  
  13. void cumulativeSum(){
  14.     cuS[1] = log((double)1);
  15.     for(int i = 2; i < S; i++){
  16.         cuS[i] = cuS[i-1] + log((double)i);
  17.     }
  18. }
  19.  
  20. int main(){
  21.     int  t, cs = 0, n, base;
  22.     cumulativeSum();
  23.     scanf("%d"&t);
  24.     while(t--){
  25.         scanf("%d %d"&n, &base);
  26.         double value = cuS[n];
  27.         value /= log((double)base);
  28.         ll ans = value;
  29.         ans += 1;
  30.         printf("Case %d: %lld\n"++cs, ans);
  31.     }
  32.     return 0;
  33. }

LightOJ - 1035 - Intelligent Factorial Factorization

  1. /****************#####    بِسْمِ اللَّهِ الرَّحْمَنِ الرَّحِيم   #####******************
  2. __________________________________________________________________________
  3. ######################  Ya-Seen Arafat(ACWizard) #########################
  4. ######################        UAP-CSE-33B        #########################
  5. *************************************************************************/
  6. #include <bits/stdc++.h>
  7. #define ll long long
  8. #define S 103
  9. using namespace std;
  10.  
  11. int prime[S];
  12. int primeNum[S];
  13. int ans[S];
  14. int ind;
  15.  
  16. void seive(){
  17.     int sqr = sqrt(S);
  18.     for(int i = 2; i <= sqr; i++){
  19.         if(prime[i]){
  20.             for(int j = i+i; j < S; j+=i)prime[j] = 0;
  21.         }
  22.     }
  23.     ind = 0;
  24.     for(int i = 2; i < S; i++)if(prime[i])primeNum[ind++] = i;
  25. }
  26.  
  27. void numDivisor(int number){
  28.     int cn = 0;
  29.     for(int i = 0; i < ind; i++){
  30.         for(int k = 2; k <= number; k++){
  31.             while(prime[k]%primeNum[i] == 0){
  32.                 cn++;
  33.                 prime[k] /= primeNum[i];
  34.             }
  35.             ans[primeNum[i]] += cn;
  36.             cn = 0;
  37.         }
  38.     }
  39. }
  40.  
  41. int main(){
  42.     int  t, cs = 0, n;
  43.     memset(prime, 1sizeof(prime));
  44.     seive();
  45.     scanf("%d"&t);
  46.     while(t--){
  47.         scanf("%d"&n);
  48.         memset(ans, 0sizeof(ans));
  49.         for(int i = 2; i < S; i++)prime[i] = i;
  50.         numDivisor(n);
  51.         printf("Case %d:"++cs);
  52.         bool flag = true;
  53.         for(int i = 2; i <= 100; i++){
  54.             if(ans[i] && flag)printf(" %d = %d (%d)", n, i, ans[i]), flag = false;
  55.             else if(!flag && ans[i])printf(" * %d (%d)", i, ans[i]);
  56.         }
  57.         puts("");
  58.     }
  59.     return 0;
  60. }

LightOJ - Trailing Zeroes (I)

  1. /****************#####    بِسْمِ اللَّهِ الرَّحْمَنِ الرَّحِيم   #####******************
  2. __________________________________________________________________________
  3. ######################  Ya-Seen Arafat(ACWizard) #########################
  4. ######################        UAP-CSE-33B        #########################
  5. *************************************************************************/
  6. #include <bits/stdc++.h>
  7. #define ll long long
  8. #define S 1000003
  9. using namespace std;
  10.  
  11. bool prime[S];
  12. ll primeNum[S];
  13. int ind;
  14.  
  15. void seive(){
  16.     prime[0] = prime[1] = false;
  17.     int sqr = sqrt(S);
  18.     for(int i = 2; i <= sqr; i++){
  19.         if(prime[i]){
  20.             for(int j = i+i; j <= S; j+=i)prime[j] = false;
  21.         }
  22.     }
  23.     ind = 0;
  24.     for(int i = 0; i <= S; i++)if(prime[i])primeNum[ind++] = i;
  25. }
  26.  
  27. ll numDivisor(ll raw){
  28.     ll  tot = 0, prev = 0LL, cn = 0LL;
  29.     for(int i = 0; i < ind && primeNum[i]*primeNum[i] <= raw; i++){
  30.         bool flag = true;
  31.         while(raw%primeNum[i] == 0LL){
  32.             cn++;
  33.             raw /= primeNum[i];
  34.         }
  35.         tot = (prev*cn) + (prev+cn);
  36.         prev = tot;
  37.         if(raw != 1)cn = 0LL;
  38.     }
  39.     if(!cn)tot += tot + 1;
  40.     return tot;
  41. }
  42.  
  43. int main(){
  44.     int  t, cs = 0;
  45.     memset(prime, truesizeof(prime));
  46.     seive();
  47.     ll n;
  48.     scanf("%d"&t);
  49.     while(t--){
  50.         scanf("%lld"&n);
  51.         if(== 1LL){
  52.             printf("Case %d: 0\n"++cs);
  53.             continue;
  54.         }
  55.         ll ans = numDivisor(n);
  56.         printf("Case %d: %lld\n"++cs, ans);
  57.     }
  58.     return 0;
  59. }

Friday, February 26, 2016

LightOJ - 1014 - Ifter Party

  1. /****************#####    بِسْمِ اللَّهِ الرَّحْمَنِ الرَّحِيم   #####******************
  2. __________________________________________________________________________
  3. ######################  Ya-Seen Arafat(ACWizard) #########################
  4. ######################        UAP-CSE-33B        #########################
  5. *************************************************************************/
  6. #include <bits/stdc++.h>
  7. using namespace std;
  8.  
  9. int main(){
  10.     int t, P, L, cs = 0;
  11.     scanf("%d"&t);
  12.     while(t--){
  13.         scanf("%d %d"&P, &L);
  14.         int Q = P-L;
  15.         printf("Case %d: "++cs);
  16.         if(>= Q){
  17.             puts("impossible");
  18.             continue;
  19.         }
  20.         int sqr = sqrt(Q);
  21.         vector <int> container;
  22.         for(int i = 1; i <= sqr; i++){
  23.             if(Q%== 0){
  24.                 if(< i)container.push_back(i);
  25.                 int p = Q/i;
  26.                 if(!= i && L < p)container.push_back(p);
  27.             }
  28.         }
  29.         sort(container.begin(), container.end());
  30.         int sz = container.size();
  31.         if(sz == 0)puts("impossible");
  32.         else{
  33.             for(int i = 0; i < sz; i++){
  34.                 printf("%d", container[i]);
  35.                 if(== sz-1)puts("");
  36.                 else printf(" ");
  37.             }
  38.         }
  39.     }
  40.     return 0;
  41. }