Skip to main content

Posts

Showing posts from 2016

DIVFACT

DIVFACT - Divisors of factorial #include <iostream> #include <vector> using namespace std; const int m = 1000000007; const int N = 50001; vector<long int> p; void sieve() {     bool prime[N] = {false};     for(int i = 3; i * i <= N; i+= 2)     {         if(!prime[i])         {             for(int j = i * i; j <= N; j += i)             {                 prime[j] = true;             }         }     }     p.push_back(2);     for(int i = 3; i <= N; i+=2)     {         if(!prime[i])             p.push_back(i);     } } long long int findDivisor(int n) {         long long int result = 1;         int temp = n;         for(long int j = 0; j < p.size() && p[j] <= temp; j++)         {             long long int product = p[j];             long int num = 0;             while(temp / product)             {                 num += temp / product;                 product *= p[j];             }          

DIVSUM

#include <iostream> #include <cmath> using namespace std; const int N = 500001; long long int sum[N]; int main() {     long long int result = 0;     for(int i = 1; i <= N; i++)     {         result = 1;         int temp = i;         for(int j = 2; j * j <= temp; j++)         {             int num = 0;             while(temp % j == 0)             {                 num++;                 temp = temp / j;             }             if(num != 0)                 result *= (pow(j, (num + 1))  - 1)/ (j - 1); // refer http://mathschallenge.net/library/number/sum_of_divisors for logic         }         if(temp != 1)         {             result *= (temp + 1);         }         sum[i] = result;     }     int t, n;     cin >> t;     while(t--)     {         cin >> n;         cout << sum[n] - n << endl; // sum[n] - n for sum of proper divisor     }     return 0; }

DIV

#include <iostream> #include <cstring> #include <vector> using namespace std; const int N = 1001; bool prime[N] = {true}; int d[1000001];//stores number of divisor vector<int> p; void sieve() {     for(int i = 0; i <= N; i++)         prime[i] = true;     p.push_back(2);     for(int i = 2; i * i <= 1000; i++)     {         if(prime[i])         {             for(int j = i * i; j <= 1000; j += i)             {                 prime[j] = false;             }         }     }     for(int i = 3; i <= N; i += 2)     {         if(prime[i])             p.push_back(i);     } } bool isPrime(int n) {     if(n == 1)         return false;     for(int i = 0; i < p.size() && p[i] * p[i] <= n; i++)     {         if(n % p[i] == 0)             return false;     }     return true; } void divisor() {     int num; //number of divisor     int result;     for(int i = 2; i <= 1000000; i++)     {  

NICEBTRE

#include <iostream> #include <string> using namespace std; int n, i; string s; int findDepth() {     if(s[i] == 'l')         return 0;     i++;     int l = findDepth();     i++;     int r = findDepth();     return max(l, r) + 1; } int main() {     int t;     cin >> t;     cin.get();     while(t--)     {         cin >> s;         n = s.size();         i = 0;         cout << findDepth() << endl;     }     return 0; }

ONEZERO

#include <iostream> #include <queue> #define pi pair<int, string> #define mp make_pair using namespace std; bool visited[20001]; int n; string bfs() {     queue<pi> q;     q.push(mp(1, "1"));     pi top;     long long int r; //remainder     string m; //number in char     while(!q.empty())     {         top = q.front();         q.pop();         r = top.first;         m = top.second;         if(r == 0)         {             return m;         }         if(!visited[r])         {             visited[r] = true;             r *= 10;             q.push(mp(r % n, top.second + "0"));             q.push(mp((r + 1) % n, top.second + "1"));         }     } } int main() {     int k;     cin >> k;     while(k--)     {         cin >> n;         for(int i = 0; i < 20001; i++)             visited[i] = false;         cout << bfs() << endl;     }     return 0; }

PRATA

#include <iostream> #include <queue> #define FOR(i, s, t) for(i = s; i < t; i++) using namespace std; long long int freq[51], r[51], arr[51]; int main() {     int t, p, l, i;     long int a, time, c;     cin >> t;     while(t--)     {         cin >> p >> l;         priority_queue<int, vector<int>, greater<int> >pq;         FOR(i, 0, 51)         {             freq[i] = 0;             r[i] = 0;         }         FOR(i, 0, l)         {             cin >> a;             freq[i] = 1;             arr[i] = r[i] = a;             pq.push(a);         }         time = 0;//start time         c = 0;         while(1)         {             time = pq.top();             pq.pop();             while(!pq.empty() && pq.top() == time)                 pq.pop();             FOR(i, 0, l)             {                 if(arr[i] == time)                  {                      c++;