Showing posts with label math. Show all posts
Showing posts with label math. Show all posts

Sunday, July 12, 2020

POJ.2348 Euclid's Game

1.Problem
http://poj.org/problem?id=2348

2.Idea
Euclid's

3.Source
 void solve()  
 {  
      while (1) {  
           int a, b;  
           cin >> a >> b;  
           if (a == 0 && b == 0) break;  
           bool f = true;  
           while (1) {  
                if (a > b) swap(a, b);  
                if (b%a == 0) break;  
                if (b - a > a) break;  
                b -= a;  
                f = !f;  
           }  
           if (f) puts("Stan wins");  
           else puts("Ollie wins");  
      }  
 }  
 int main() {  
      ios_base::sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(13);  
      solve();  
      return 0;  
 }  

Friday, July 10, 2020

POJ.2409 Let it Bead

1.Problem
http://poj.org/problem?id=2409

2.Idea
Polya's theorem.

3.Source
 int gcd(int a, int b)  
 {  
      return b == 0 ? a : gcd(b, a%b);  
 }  
 int power(int p, int n)  
 {  
      int ans = 1;  
      while (n)  
      {  
           if (n & 1)  
                ans *= p;  
           p *= p;  
           n /= 2;  
      }  
      return ans;  
 }  
 void solve()  
 {  
      while (1) {  
           int c, s, ans = 0;  
           cin >> c >> s;  
           if (c == 0 && s == 0) break;  
           for (int i = 1; i <= s; i++)  
                ans += power(c, gcd(s, i));  
           if (s & 1)   
                ans += s*power(c, s / 2 + 1);  
           else  
                ans += (power(c, s / 2 + 1) + power(c, s / 2))*(s / 2);  
           ans /= 2 * s;  
           cout << ans << endl;  
      }  
 }  

Tuesday, July 7, 2020

POJ.1286 Necklace of Beads

1.Problem
http://poj.org/problem?id=1286

2.Idea
Polya's theorem.

3.Source
 Int p[110], ans, n;  
 Int gcd(Int a, Int b)  
 {  
      if (!(a%b)) return b;  
      return gcd(b, a%b);  
 }  
 void solve()  
 {  
      int i, j;  
      while (1) {  
           cin >> n;  
           if (n == 0) {  
                cout << 0 << endl;  
                continue;  
           }  
           if (n == -1) return;  
           p[0] = 1;  
           for (int i = 0; i < n; i++) p[i + 1] = p[i] * 3;  
           if (!(n % 2)) ans = (n / 2) * (p[n / 2 + 1] + p[n / 2]);  
           else ans = n * p[n / 2 + 1];  
           for (int i = 1; i <= n; i++) ans += p[gcd(i, n)];  
           ans /= 2 * n;  
           cout << ans << endl;  
      }  
 }  

Monday, July 6, 2020

POJ.2407 Relatives

1.Problem
http://poj.org/problem?id=2407

2.Idea
Prime Factorization + Inclusion-exclusion principle

3.Source
 vector<int> a;  
 int n, m;  
 Int gcd(const Int a, const Int b)  
 {  
      if (b == 0) return a;  
      return gcd(b, a%b);  
 }  
 void pFac(Int H)  
 {  
      for (Int i = 2; i*i <= H; i++) {  
           int cnt = 0;  
           while (H%i == 0) {  
                cnt++;  
                H /= i;  
           }  
           if (cnt > 0) {  
                a.push_back(i);  
           }  
      }  
      if (H > 1) {  
           a.push_back(H);  
      }  
      m = a.size();  
 }  
 void solve()  
 {  
      while (1) {  
           scanf("%lld", &n);  
           if (n == 0) break;  
           a.clear();  
           pFac(n);  
           Int res = 0;  
           for (int i = 1; i < (1 << m); i++) {  
                int num = 0;  
                for (int j = i; j != 0; j >>= 1) {  
                     num += j & 1;  
                }  
                Int lcm = 1;  
                for (int j = 0; j < m; j++) {  
                     if (i >> j & 1) {  
                          lcm = lcm / gcd(lcm, a[j]) * a[j];  
                          if (lcm > n) break;  
                     }  
                }  
                if (num % 2 == 0) res -= n / lcm;  
                else res += n / lcm;  
           }  
           printf("%d\n", n - res);  
      }  
 }  

Sunday, July 5, 2020

POJ.3532 Resistance

1.Problem
http://poj.org/problem?id=3532

2.Idea
See this blog.

3.Source
 typedef vector<double> vec;  
 typedef vector<vec> mat;  
 double resistor[MAX_N][MAX_N];    
 int N, M;  
 vec gauss(const mat&A, const vec&b) {  
   int n = A.size();//!!!!  
   mat B(n, vec(n + 1));  
   for (int i = 0; i < n; i++)  
     for (int j = 0; j < n; j++)B[i][j] = A[i][j];  
   for (int i = 0; i < n; i++)B[i][n] = b[i];  
   for (int i = 0; i < n; i++) {  
     int pivot = i;  
     for (int j = i; j < n; j++) {  
       if (abs(B[j][i]) > abs(B[pivot][i])) {//!!!  
         pivot = j;  
       }  
     }  
     swap(B[i], B[pivot]);  
     if (abs(B[i][i]) < EPS)return vec(); 
     for (int j = i + 1; j <= n; j++) {  
       B[i][j] /= B[i][i];  
     }  
     for (int j = 0; j < n; j++) {  
       if (i != j) {  
         for (int k = i + 1; k <= n; k++) {  
           B[j][k] -= B[j][i] * B[i][k];  
         }  
       }  
     }  
   }  
   vec x(n);  
   for (int i = 0; i < n; i++) {  
     x[i] = B[i][n];  
   }  
   return x;  
 }  
 int main() {  
   while (scanf("%d%d", &N, &M) != EOF) {  
     memset(resistor, 0, sizeof(resistor));  
     for (int i = 0; i < M; i++) {  
       int from, to;  
       double R;  
       scanf("%d%d%lf", &from, &to, &R);  
       if (R == 0)continue;  
       from--, to--;  
       resistor[from][to] += 1 / R;  
       resistor[to][from] += 1 / R;  
     }  
     for (int i = 0; i < N; i++) {  
       for (int j = 0; j < N; j++) {  
         resistor[i][j] = 1.0 / resistor[i][j];  
       }  
     }  
     mat A(N, vec(N, 0));  
     vec b(N, 0);  
     b[0] = 1.0;  
     b[N - 1] = 0.0;  
     A[0][0] = 1, A[N - 1][N - 1] = 1;  
     for (int i = 1; i < N - 1; i++) {  
       for (int j = 0; j < N; j++) {  
         if (resistor[i][j] > 0) {  
           double I = 1.0 / resistor[i][j];  
           A[i][i] -= I;  
           A[i][j] += I;  
         }  
       }  
     }  
     vec voltage = gauss(A, b);  
     double current = 0;  
     for (int i = 0; i < N; i++) {  
       if (resistor[0][i] > 0) {  
         current += (voltage[0] - voltage[i]) / resistor[0][i];  
       }  
     }  
     printf("%.2f\n", 1.0 / current);  
   }  
   return 0;  
 }  

POJ.2345 Central heating

1.Problem
http://poj.org/problem?id=2345

2.Idea
Solve the linear equation by Gaussian method.

3.Source
typedef vector vec;
typedef vector mat;
vec gauss(const mat &A, const vec &b)  
 {  
      int n = A.size();  
      mat B(n, vec(n + 1));  
      for (int i = 0; i < n; i++)  
           for (int j = 0; j < n; j++)B[i][j] = A[i][j];  
      for (int i = 0; i < n; i++)B[i][n] = b[i];  
      for (int i = 0; i < n; i++) {  
           int pivot = i;  
           for (int j = i; j < n; j++) {  
                if (B[j][i] > B[pivot][i]) {//!!!  
                     pivot = j;  
                }  
           }  
           swap(B[i], B[pivot]);  
           if (B[i][i] < EPS)return vec();  
           for (int j = 0; j < n; j++) {  
                if (i != j) {  
                     for (int k = i + 1; k <= n; k++) {  
                          B[j][k] ^= B[j][i] & B[i][k];  
                     }  
                }  
           }  
      }  
      vec x(n);  
      for (int i = 0; i < n; i++) {  
           x[i] = B[i][n];  
      }  
      return x;  
 }  
 int n;  
 void solve()  
 {  
      while (scanf("%d", &n) != EOF) {  
           vec b(n);  
           mat A(n, vec(n));  
           for (int i = 0; i < n; i++) {  
                b[i] = 1;  
           }  
           for (int i = 0; i < n; i++) {  
                while (1) {  
                     int a;  
                     scanf("%d", &a);  
                     if (a == -1) break;  
                     a--;  
                     A[a][i] = 1;  
                }  
           }  
           vec x = gauss(A, b);  
           bool flag = 0;  
           for (int i = 0; i < x.size(); i++) {  
                if (x[i]) {  
                     if (!flag) {  
                          flag = 1;  
                          printf("%d", i + 1);  
                     }  
                     else {  
                          printf(" %d", i + 1);  
                     }  
                }  
           }  
           puts("");  
      }  
 }  

Sunday, June 21, 2020

POJ.2115 C Looooops

1.Problem
http://poj.org/problem?id=2115

2.Idea
ext gcd

3.Source
 Int a, b, c, k;  
 Int m;  
 // a x + b y = gcd(a, b)  
 Int extgcd(Int a, Int b, Int &x, Int &y) {  
      Int g = a; x = 1; y = 0;  
      if (b != 0) g = extgcd(b, a % b, y, x), y -= (a / b) * x;  
      return g;  
 }  
 void solve()  
 {  
      while (scanf("%lld%lld%lld%lld", &a, &b, &c, &k) > 0) {  
           if (a == 0 && b == 0 && c == 0 && k == 0) {  
                break;  
           }  
           Int m = (Int)1 << k, x, y;  
           Int g = extgcd(c, m, x, y);  
           if ((b - a) % g) {  
                printf("FOREVER\n");  
           }  
           else {  
                x = (x*((b - a) / g) % (m / g) + (m / g)) % (m / g);  
                printf("%lld\n", x);  
           }  
      }  
 }  
 int main() {  
      ios_base::sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(13);  
      solve();  
      return 0;  
 }  

Saturday, June 20, 2020

POJ.1284 Primitive Roots

1.Problem
http://poj.org/problem?id=1284

2.Idea
Primitive roots, Euler function

3.Source
 int euler[N];  
 void solve()  
 {  
      for (int i = 0; i < N; i++) euler[i] = i;  
      for (int i = 2; i < N; i++) {  
           if (euler[i] == i) {  
                for (int j = i; j < N; j += i) {  
                     euler[j] = euler[j] / i * (i - 1);  
                }  
           }  
      }  
      int p;  
      while (scanf("%d", &p)>0) {  
           printf("%d\n", euler[p - 1]);  
      }  
 }  
 int main() {  
      ios_base::sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(13);  
      solve();  
      return 0;  
 }  

Saturday, May 30, 2020

Codeforces.1359E Modular Stability

1.Problem
https://codeforces.com/contest/1359/problem/E

2.Idea
a1 must be a divisor of a2..aN.

3.Source
 Int fact[N], invfact[N], inv[N];  
 void init()  
 {  
      fact[0] = 1;  
      for (int i = 1; i < N; i++) {  
           fact[i] = fact[i - 1] * i % MOD;  
      }  
      inv[1] = 1;  
      for (int i = 2; i < N; i++) {  
           inv[i] = -inv[MOD%i] * (MOD / i) % MOD;  
           if (inv[i] < 0) inv[i] += MOD;  
      }  
      invfact[0] = 1;  
      for (int i = 1; i < N; i++) {  
           invfact[i] = invfact[i - 1] * inv[i] % MOD;  
      }  
 }  
 Int nCk(Int n, Int k)  
 {  
      return fact[n] * invfact[k] % MOD * invfact[n - k] % MOD;  
 }  
 Int nPk(Int n, Int k)  
 {  
      return fact[n] * invfact[n - k] % MOD;  
 }  
 Int n, k;  
 int main()  
 {  
      cin >> n >> k;  
      init();  
      Int ans = 0;  
      for (int i = 1; i <= n; i++) {  
           int num = (n / i);  
           if (num >= k) {  
                ans = (ans + nCk(num - 1, k - 1)) % MOD;  
           }  
      }  
      cout << ans << endl;  
      return 0;  
 }  

Tuesday, May 12, 2020

Codeforces.1349B Orac and Medians

1.Problem
https://codeforces.com/problemset/problem/1349/B

2.Idea
Prime Factorization

3.Source
 int n;  
 ll a[N];  
 map<ll, vector<int>> mp;  
 void pFac(ll H)  
 {  
      for (ll i = 2; i*i <= H; i++) {  
           int cnt = 0;  
           while (H%i == 0) {  
                cnt++;  
                H /= i;  
           }  
           if (cnt > 0) {  
                mp[i].push_back(cnt);  
           }  
      }  
      if (H > 1) {  
           mp[H].push_back(1);  
      }  
 }  
 ll gcd(const ll a, const ll b)  
 {  
      if (b == 0) return a;  
      return gcd(b, a%b);  
 }  
 int main() {  
      scanf("%d\n", &n);  
      for (int i = 0; i < n; i++) {  
           scanf("%d", &a[i]);  
           pFac(a[i]);  
      }  
      if (n == 2) {  
           cout << a[0] * a[1] / gcd(a[0] , a[1]) << endl;  
           return 0;  
      }  
      ll ans = 1;  
      for (auto itr = mp.begin(); itr != mp.end(); itr++) {  
           int p = itr->first;  
           vector<int> v = itr->second;  
           int k = 0;  
           if (v.size() + 2 <= n) continue;  
           sort(v.begin(), v.end());  
           if (v.size() + 1 == n) {  
                k = v[0];  
           }  
           else {  
                k = v[1];  
           }  
           int t = (int)pow(1.0 * p, 1.0 * k);  
           ans = ans * (ll)t;  
      }  
      printf("%d\n", ans);  
      return 0;  
 }  

Tuesday, May 5, 2020

POJ.1012 Joseph

1.Problem
http://poj.org/problem?id=1012

2.Idea
https://en.wikipedia.org/wiki/Josephus_problem

3.Source
 int Joseph[15];  
 int k;  
 int main()  
 {  
      while (scanf("%d", &k) && k) {  
           if (Joseph[k]) {  
                printf("%d\n", Joseph[k]);  
                continue;  
           }  
           int n = 2 * k, m = 1;  
           int flag[30] = { 0 };  
           for (int i = 1; i <= k; i++) {  
                flag[i] = (flag[i - 1] + m - 1) % (n - i + 1);  
                if (flag[i] < k) {  
                     i = 0;  
                     m++;  
                }  
           }  
           Joseph[k] = m;  
           printf("%d\n", m);  
      }  
      return 0;  
 }  

Monday, April 6, 2020

POJ.1019 Number Sequence

1.Problem
http://poj.org/problem?id=1019

2.Idea
Enumeration

3.Source
 #define MAX_N 35000  
 ll a[MAX_N];  
 ll b[MAX_N];  
 int n;  
 int getDigNum(int k)  
 {  
      if (k < 1) return 0;  
      int ans = 0;  
      while (k) {  
           ans++;  
           k /= 10;  
      }  
      return ans;  
 }  
 void solve()  
 {  
      if (n == 1) {  
           cout << 1 << endl;  
           return;  
      }  
      if (n == 2) {  
           cout << 1 << endl;  
           return;  
      }if (n == 3) {  
           cout << 2 << endl;  
           return;  
      }  
      int i1 = 1, i2 = 1;  
      //get group  
      while (b[i1] <= n) i1++;  
      i1--;  
      if (b[i1] == n) {  
           cout << i1 % 10 << endl;  
           return;  
      }  
      else {  
           n = n - b[i1];  
      }  
      //get number  
      while (a[i2] <= n) i2++;  
      i2--;  
      if (a[i2] == n) {  
           cout << i2 % 10 << endl;  
           return;  
      }  
      else {  
           n = n - a[i2];  
      }  
      //get digit  
      int h = i2 + 1;  
      n = getDigNum(h) - n;  
      h = h / (int)pow(10.0, n);  
      cout << h % 10 << endl;  
 }  
 int main()  
 {       
      for (int i = 1; i < MAX_N; i++) {  
           a[i] = a[i - 1] + getDigNum(i);  
           b[i] = b[i - 1] + a[i];  
      }  
      int t = 0;  
      cin >> t;  
      while (t--) {  
           cin >> n;  
           solve();  
      }  
      return 0;  
 }  

Sunday, April 5, 2020

POJ.1338 Ugly Numbers

1.Problem
http://poj.org/problem?id=1338

2.Idea
Using 3 variables

3.Source
 ll ans[1502];  
 int main()  
 {  
      ans[1] = 1;  
      int u2 = 1, u3 = 1, u5 = 1;  
      for (int i = 2; i <= 1500; i++) {  
           int value2 = ans[u2] * 2;  
           int value3 = ans[u3] * 3;  
           int value5 = ans[u5] * 5;  
           ans[i] = min(min(value2, value3), value5);  
           if (ans[i] == value2) u2++;  
           if (ans[i] == value3) u3++;  
           if (ans[i] == value5) u5++;  
      }  
      int n;  
      while ((cin >> n), n)  
      {  
           cout << ans[n] << endl;  
      }  
      return 0;  
 }  

POJ.3006 Dirichlet's Theorem on Arithmetic Progressions

1.Problem
http://poj.org/problem?id=3006

2.Idea
Sieve of Eratosthenes

3.Source
 bool isPrime[1000006];  
 vector<int> Primes;  
 int a, d, n;  
 //Sieve of Eratosthenes  
 void prep()  
 {  
      for (int i = 2; i < 1000006; i++) {  
           if (isPrime[i]==0) {  
                Primes.push_back(i);  
                for (int j = i * 2; j < 1000006; j += i) {  
                     isPrime[j] = 1;  
                }  
           }  
      }  
 }  
 int main()  
 {  
      prep();  
      while (scanf("%d%d%d", &a, &d, &n) > 0) {  
           if (a + d + n == 0) break;  
           int cnt = 0;  
           for (int i = 0; i < Primes.size(); i++) {  
                if (Primes[i] >= a && (Primes[i] - a) % d == 0) {  
                     cnt++;  
                     if (cnt == n) {  
                          cout << Primes[i] << endl;  
                          break;  
                     }  
                }  
           }  
      }  
      return 0;  
 }  

Wednesday, April 1, 2020

POJ.2739 Sum of Consecutive Prime Numbers

1.Problem
http://poj.org/problem?id=2739

2.Idea
Enumerating primes

3.Source
 const int maxp = 2000;  
 int n = 10000;  
 int prime[maxp], total = 0;  
 bool isPrime(int k)  
 {  
      for (int i = 0; i < total; i++) {  
           if (k % prime[i] == 0) return false;  
      }  
      return true;  
 }  
 int main()  
 {  
      for (int i = 2; i <= n; i++) {  
           if (isPrime(i)) prime[total++] = i;  
      }  
      prime[total] = n + 1;  
      int m;  
      cin >> m;  
      while (m) {  
           int ans = 0;  
           for (int i = 0; m >= prime[i]; i++) {  
                int cnt = 0;  
                for (int j = i; j < total && cnt < m; j++) {  
                     cnt += prime[j];  
                }  
                if (cnt == m) ans++;  
           }  
           cout << ans << endl;  
           cin >> m;  
      }  
      return 0;  
 }  

Wednesday, February 12, 2020

POJ.2429 GCD & LCM Inverse

1.Problem
http://poj.org/problem?id=2429

2.Idea
this one was above than my level so quoted a very nice blog here.

3.Source
 #include <iostream>  
 #include <vector>  
 #include <map>  
 #include <cstdlib>  
 using namespace std;  
 typedef long long LL;  
 // return (a * b) % m  
 LL mod_mult(LL a, LL b, LL m) {  
  LL res = 0;  
  LL exp = a % m;  
  while (b) {  
   if (b & 1) {  
    res += exp;  
    if (res > m) res -= m;  
   }  
   exp <<= 1;  
   if (exp > m) exp -= m;  
   b >>= 1;  
  }  
  return res;  
 }  
 // return (a ^ b) % m  
 LL mod_exp(LL a, LL b, LL m) {  
  LL res = 1;  
  LL exp = a % m;  
  while (b) {  
   if (b & 1) res = mod_mult(res, exp, m);  
   exp = mod_mult(exp, exp, m);  
   b >>= 1;  
  }  
  return res;  
 }  
 // ミラー-ラビン素数判定法  
 bool miller_rabin(LL n, LL times) {  
  if (n < 2) return false;  
  if (n == 2) return true;  
  if (!(n & 1)) return false;  
  LL q = n-1;  
  int k = 0;  
  while (q % 2 == 0) {  
   k++;  
   q >>= 1;  
  }  
  // n - 1 = 2^k * q (qは奇素数)  
  // nが素数であれば、下記のいずれかを満たす  
  // (i) a^q ≡ 1 (mod n)  
  // (ii) a^q, a^2q,..., a^(k-1)q のどれかがnを法として-1  
  //  
  // なので、逆に(i)(ii)いずれも満たしていない時は合成数と判定できる  
  //  
  for (int i = 0; i < times; i++) {  
   LL a = rand() % (n-1) + 1; // 1,..,n-1からランダムに値を選ぶ  
   LL x = mod_exp(a, q, n);  
   // (i)をチェック  
   if (x == 1) continue;  
   // (ii)をチェック  
   bool found = false;  
   for (int j = 0; j < k; j++) {  
    if (x == n-1) {  
     found = true;  
     break;  
    }  
    x = mod_mult(x, x, n);  
   }  
   if (found) continue;  
   return false;  
  }  
  return true;  
 }  
 LL get_gcd(LL n, LL m) {  
  if (n < m) swap(n, m);  
  while (m) {  
   swap(n, m);  
   m %= n;  
  }  
  return n;  
 }  
 // ポラード・ロー素因数分解法  
 LL pollard_rho(LL n, int c) {  
  LL x = 2;  
  LL y = 2;  
  LL d = 1;  
  while (d == 1) {  
   x = mod_mult(x, x, n) + c;  
   y = mod_mult(y, y, n) + c;  
   y = mod_mult(y, y, n) + c;  
   d = get_gcd((x-y >= 0 ? x-y : y-x), n);  
  }  
  if (d == n) return pollard_rho(n, c+1);  
  return d;  
 }  
 const int MAX_PRIME = 200000;  
 vector<int> primes;  
 vector<bool> is_prime;  
 // 小さい素数(MAX_PRIMEまで)は先に用意しとく  
 void init_primes() {  
  is_prime = vector<bool>(MAX_PRIME + 1, true);  
  is_prime[0] = is_prime[1] = false;  
  for (int i = 2; i <= MAX_PRIME; i++) {  
   if (is_prime[i]) {  
    primes.push_back(i);  
    for (int j = i*2; j <= MAX_PRIME; j+=i) {  
     is_prime[j] = false;  
    }  
   }  
  }  
 }  
 // 素数かどうか判定。大きければミラーラビンを使う  
 bool isPrime(LL n) {  
  if (n <= MAX_PRIME) return is_prime[n];  
  else return miller_rabin(n, 20);  
 }  
 // 素因数分解する。小さい数は用意した素数で試し割り、大きければポラード・ロー  
 void factorize(LL n, map<LL, int>& factors) {  
  if (isPrime(n)) {  
   factors[n]++;  
  } else {  
   for (int i = 0; i < primes.size(); i++) {  
    int p = primes[i];  
    while (n % p == 0) {  
     factors[p]++;  
     n /= p;  
    }  
   }  
   if (n != 1) {  
    if (isPrime(n)) {  
     factors[n]++;  
    } else {  
     LL d = pollard_rho(n, 1);  
     factorize(d, factors);  
     factorize(n/d, factors);  
    }  
   }  
  }  
 }  
 pair<LL, LL> solve(LL a, LL b) {  
  LL c = b / a;  
  map<LL, int> factors;  
  factorize(c, factors);  
  vector<LL> mult_factors;  
  for (map<LL, int>::iterator it = factors.begin(); it != factors.end(); it++) {  
   LL mul = 1;  
   while (it->second) {  
    mul *= it->first;  
    it->second --;  
   }  
   mult_factors.push_back(mul);  
  }  
  LL best_sum = 1e18, best_x = 1, best_y = c;  
  for (int mask = 0; mask < (1 << mult_factors.size()); mask++) {  
   LL x = 1;  
   for (int i = 0; i < mult_factors.size(); i++) {  
    if (mask & (1 << i)) x *= mult_factors[i];  
   }  
   LL y = c / x;  
   if (x < y && x + y < best_sum) {  
    best_sum = x + y;  
    best_x = x;  
    best_y = y;  
   }  
  }  
  return make_pair(best_x * a, best_y * a);  
 }  
 int main() {  
  cin.tie(0);  
  ios::sync_with_stdio(false);  
  init_primes();  
  LL a, b;  
  while (cin >> a >> b) {  
   pair<LL, LL> ans = solve(a, b);  
   cout << ans.first << " " << ans.second << endl;  
  }  
 }  

Tuesday, February 11, 2020

POJ.1930 Dead Fraction

1.Problem
http://poj.org/problem?id=1930

2.Idea
All credit to this.

3.Source
 int gcd(const int a, const int b)  
 {  
      if (b == 0) return a;  
      return gcd(b, a%b);  
 }  
 static char str[1000];  
 int main()  
 {  
      int sum, last, k, c, i, a, b, div, min_b, min_a, length;  
      while (~scanf("%s", str))  
      {  
           if (strlen(str) == 1 && str[0] == '0')  
                break;  
           sum = 0; length = 0; min_b = INT_MAX;  
           for (i = 2; str[i] != '.'; i++)  
           {  
                sum = sum * 10 + str[i] - '0';  
                length++;  
           }  
           c = (int)pow(10.0, length);  
           for (i = 1, last = sum, k = 1; i <= length; i++)  
           {  
                last /= 10; k *= 10; c /= 10;  
                a = sum - last;  
                b = c*(k - 1);  
                div = gcd(a, b);  
                if (b / div < min_b)  
                {  
                     min_a = a / div;  
                     min_b = b / div;  
                }  
           }  
           printf("%d/%d\n", min_a, min_b);  
      }  
      return 0;  
 }  

Saturday, February 8, 2020

POJ.3292 Semi-prime H-numbers

1.Problem
http://poj.org/problem?id=3292

2.Idea
Sieve of Eratosthenes

3.Source
 bool isPrime[1000006];  
 vector<ll> p;  
 int dp[1000006];  
 void prep()  
 {  
      for (int i = 5; i < 1000006; i += 4) {  
           if (!isPrime[i]) {  
                p.push_back(i);  
                for (int j = i * 5; j < 1000006; j += i*4) {  
                     isPrime[j] = true;  
                }  
           }  
      }  
      int n = p.size();  
      for (int i = 0; i < n; i++)  
           for (int j = i; j < n && (p[i] * p[j]) < 1000006; j++) {  
                int k = p[i] * p[j];  
                dp[k] = 1;  
           }  
      for (int i = 1; i < 1000006; i++) {  
           dp[i] += dp[i - 1];  
      }  
 }  
 void solve()  
 {  
      int h;  
      while (1) {  
           cin >> h;  
           if (h == 0) break;  
           cout << h << " " << dp[h] << endl;  
      }  
 }  
 int main()  
 {  
      prep();  
      solve();  
      return 0;  
 }  

POJ.3421 X-factor Chains

1.Problem
http://poj.org/problem?id=3421

2.Idea
Prime number factorize.

3.Source
 ll fac[20];  
 void solve(int x)  
 {  
      map<int, int> res;  
      for (int i = 2; i*i <= x; i++) {  
           while (x%i == 0) {  
                ++res[i];  
                x /= i;  
           }  
      }  
      if (x != 1) res[x] = 1;  
      fac[0] = 1;  
      fac[1] = 1;  
      for (int i = 2; i < 20; i++) {  
           fac[i] = fac[i - 1] * (ll)i;  
      }  
      if (res.size() == 1) {  
           cout << res.begin()->second << " 1" << endl;  
           return;  
      }  
      //calc  
      ll total = 0, dup = 1;  
      for (map<int, int>::iterator itr = res.begin(); itr != res.end(); itr++) {  
           total += itr->second;  
           dup = dup * fac[itr->second];  
      }  
      cout << total << " " << fac[total] / dup << endl;  
 }  
 int main()  
 {  
      int x;  
      while (scanf("%d", &x) > 0) {  
           solve(x);  
      }  
      return 0;  
 }  

POJ.3126 Prime Path

1.Problem
http://poj.org/problem?id=3126

2.Idea
Sieve of Eratosthenes

3.Source
 bool isPrime[10000];  
 bool visited[10000];  
 int n;  
 int s, t;  
 vector<int> GetNei(int cur)  
 {  
      vector<int> ans;  
      for (int i = 0; i < 10; i++) {  
           int tmp = cur - cur % 10 + i;  
           if (tmp != cur && isPrime[tmp] && !visited[tmp]) ans.push_back(tmp);  
           tmp = cur - cur % 100 + cur % 10 + 10 * i;  
           if (tmp != cur && isPrime[tmp] && !visited[tmp]) ans.push_back(tmp);  
           tmp = cur - cur % 1000 + cur % 100 + 100 * i;  
           if (tmp != cur && isPrime[tmp] && !visited[tmp]) ans.push_back(tmp);  
           tmp = cur % 1000 + 1000 * i;  
           if (i > 0 && tmp != cur && isPrime[tmp] && !visited[tmp]) ans.push_back(tmp);  
      }  
      return ans;  
 }  
 int solve()  
 {  
      for (int i = 0; i < 10000; i++) isPrime[i] = true;  
      isPrime[0] = isPrime[1] = false;  
      for (int i = 2; i < 10000; i++) {  
           if (isPrime[i]) {  
                for (int j = 2 * i; j < 10000; j += i) {  
                     isPrime[j] = false;  
                }  
           }  
      }  
      cin >> n;  
      while (n--) {  
           memset(visited, 0, sizeof visited);  
           cin >> s >> t;  
           queue<pair<int, int>> q;  
           q.push(make_pair(s, 0));  
           while (!q.empty()) {  
                int cur = q.front().first, d = q.front().second; q.pop();  
                if (cur == t) {  
                     cout << d << endl;  
                     break;  
                }  
                //cout << cur << endl;  
                visited[cur] = true;  
                vector<int> neibors = GetNei(cur);  
                for (int i = 0; i < neibors.size(); i++) {  
                     q.push(make_pair(neibors[i], d + 1));  
                }  
           }  
      }  
 }  
 int main()  
 {  
      solve();  
      return 0;  
 }