Advertisement
_no0B

Untitled

Apr 25th, 2019
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 54.15 KB | None | 0 0
  1. /// matrix
  2. #define MOD 1000000007
  3. struct Matrix{
  4.     int row, col;
  5.     int ar[101][101]; /// Change matrix size here, also change to long long for safety
  6.     Matrix(){} ///Beware if matrix can contain negative numbers in matrix exponentiation problems
  7.     Matrix(int n, int m, int diagonal = 0){
  8.         clr(ar);
  9.         row = n, col = m;
  10.         for (int i = min(n, m) - 1; i >= 0; i--) ar[i][i] = diagonal;
  11.     }
  12.     /// To multiply two matrices A and B, the number of columns in A must equal the number of rows in B
  13.     Matrix operator* (const Matrix& other) const{ /// hash = 709758
  14.         int i, j, k;
  15.         Matrix res(row, other.col);
  16.         long long x, y = (long long)MOD * MOD;
  17.         for(i = 0; i < row; i++){
  18.             for(j = 0; j < other.col; j++){
  19.                 for(k = 0, x = 0; k < col; k++){
  20.                     x += ((long long)ar[i][k] * other.ar[k][j]); /// replace matrix other with its transpose matrix to reduce cache miss
  21.                     if (x >= y) x -= y;
  22.                 }
  23.                 res.ar[i][j] = x % MOD;
  24.             }
  25.         }
  26.         return res;
  27.     }
  28.     Matrix operator^ (long long n) const{
  29.         Matrix x = *this, res = Matrix(row, col, 1);
  30.         while (n){
  31.             if (n & 1) res = res * x;
  32.             n = n >> 1, x = x * x;
  33.         }
  34.         return res;
  35.     }
  36.     /// Transpose matrix, T[i][j] = ar[j][i]
  37.     Matrix transpose(){
  38.         Matrix res = Matrix(col, row);
  39.         for (int i = 0; i < row; i++){
  40.             for (int j = 0; j < col; j++){
  41.                 res.ar[j][i] = ar[i][j];
  42.             }
  43.         }
  44.         return res;
  45.     }
  46.     /// rotates the matrix 90 degrees clockwise
  47.     Matrix rotate(){
  48.         Matrix res = this->transpose();
  49.         for (int i = 0; i < res.row; i++) reverse(res.ar[i], res.ar[i] + res.col);
  50.         return res;
  51.     }
  52.     inline void print(){
  53.         for (int i = 0; i < row; i++){
  54.             for (int j = 0; j < col; j++){
  55.                 printf("%d%c", ar[i][j], ((j + 1) == col) ? 10 : 32);
  56.             }
  57.         }
  58.     }
  59. };
  60. int main(){
  61.     Matrix a = Matrix(4, 5, 1);
  62.     int k = 0;
  63.     for (int i = 0; i < a.row; i++){
  64.         for (int j = 0; j < a.col; j++){
  65.             a.ar[i][j] = ++k;
  66.         }
  67.     }
  68.     a.print();
  69.     puts("");
  70.     Matrix b = a.rotate();
  71.     b.print();
  72.     return 0;
  73.     Matrix x = Matrix(5, 5, 5);
  74.     Matrix y = x ^ 5;
  75.     x.print();
  76.     y.print();
  77. }
  78.  
  79.  
  80. /***
  81.  
  82. Sometimes we may need to maintain more than one recurrence, where they are interrelated.
  83. For example, let a recurrence relation be:
  84. g(n) = 2g(n-1) + 2g(n-2) + f(n), where, f(n) = 2f(n-1) + 2f(n-2).
  85. Here, recurrence g(n) is dependent upon f(n) and the can be calculated in the same matrix
  86. but of increased dimensions. Lets design the matrices A, B then we'll try to find matrix M.
  87.  
  88.  
  89.                         Matrix A                Matrix B
  90.  
  91.                         |  g(n)  |              | g(n+1) |
  92.                         | g(n-1) |              |  g(n)  |
  93.                         | f(n+1) |              | f(n+2) |
  94.                         |  f(n)  |              | f(n+1) |
  95.  
  96.  
  97. Here, g(n+1) = 2g(n) + 2g(n-1) + f(n+1) and f(n+2) = 2f(n+1) + 2f(n).
  98. Now, using the above process, we can generate the objective matrix M as follows:
  99.  
  100. | 2  2  1  0 |
  101. | 1  0  0  0 |
  102. | 0  0  2  2 |
  103. | 0  0  1  0 |
  104.  
  105.  
  106. /// Matrix Rotations:
  107.  
  108. Rotate by +90:
  109. Transpose
  110. Reverse each row
  111.  
  112. Rotate by -90:
  113. Transpose
  114. Reverse each column
  115.  
  116.  
  117. Rotate by +180:
  118. Method 1: Rotate by +90 twice
  119. Method 2: Reverse each row and then reverse each column
  120.  
  121. Rotate by -180:
  122. Method 1: Rotate by -90 twice
  123. Method 2: Reverse each column and then reverse each row
  124. Method 3: Reverse by +180 as they are same
  125.  
  126. ***/
  127.  
  128.  
  129.  
  130. /// Lucas theorem to calculate binomial co-efficients modulo a prime
  131. #define MAXP 100010
  132. namespace lc{
  133.     int MOD = 1000000007;
  134.     int fact[MAXP], inv[MAXP];
  135.     /// Call once with the modulo prime
  136.     void init(int prime){
  137.         MOD = prime;
  138.         fact[0] = 1, inv[MOD - 1] = MOD - 1;
  139.         for (int i = 1; i < MOD; i++) fact[i] = ((long long)fact[i - 1] * i) % MOD;
  140.         for (int i = MOD - 2; i >= 0; i--) inv[i] = ((long long)inv[i + 1] * (i + 1)) % MOD;
  141.     }
  142.     inline int count(int n, int k){
  143.         if (k > n) return 0;
  144.         int x = ((long long)inv[n - k] * inv[k]) % MOD;
  145.         return ((long long)x * fact[n]) % MOD;
  146.     }
  147.     /// Lucas theorem, calculates binomial(n, k) modulo MOD, MOD must be a prime
  148.     inline int binomial(long long n, long long k){
  149.         if (k > n) return 0;
  150.         int res = 1;
  151.         k = min(k, n - k);
  152.         while (k && res){
  153.             res = ((long long)res * count(n % MOD, k % MOD)) % MOD;
  154.             n /= MOD, k /= MOD;
  155.         }
  156.         return res;
  157.     }
  158.     /*** Alternate and extended functionalities ***/
  159.     /// Must call init with prime before (Or set lc::MOD = prime)
  160.     /// Computes (n! / (p ^ (n / p))) % p in O(p log(n)) time, p MUST be a prime
  161.     /// That is, calculating n! without p's powers
  162.     /// For instance factmod(9, 3) = (1 * 2 * 4 * 5 * 2 * 7 * 8 * 1) % 3 = 1
  163.     inline int factmod(long long n, int p){
  164.         int i, res = 1;
  165.         while (n > 1) {
  166.             if ((n / p) & 1) res = ((long long)res * (p - 1)) % p;
  167.             for (i = n % p; i > 1; i--) res = ((long long)res * i) % p;
  168.             n /= p;
  169.         }
  170.         return (res % p);
  171.     }
  172.     inline int expo(int a, int b){
  173.         int res = 1;
  174.         while (b){
  175.             if (b & 1) res = (long long)res * a % MOD;
  176.             a = (long long)a * a % MOD;
  177.             b >>= 1;
  178.         }
  179.         return res;
  180.     }
  181.     /// Trailing zeros of n! in base p, p is a prime
  182.     inline long long fact_ctz(long long n, long long p){
  183.         long long x = p, res = 0;
  184.         while (n >= x){
  185.             res += (n / x);
  186.             x *= p;
  187.         }
  188.         return res;
  189.     }
  190.     /// Calculates binomial(n, k) modulo MOD, MOD must be a prime
  191.     inline int binomial2(long long n, long long k){
  192.         if (k > n) return 0;
  193.         if (fact_ctz(n, MOD) != (fact_ctz(k, MOD) + fact_ctz(n - k, MOD))) return 0;
  194.         int a = factmod(n - k, MOD), b = factmod(k, MOD), c = factmod(n, MOD);
  195.         int x = ((long long)expo(a, MOD - 2) * expo(b, MOD - 2)) % MOD;
  196.         return ((long long)x * c) % MOD;
  197.     }
  198. }
  199. int main(){
  200.     lc::init(997);
  201.     printf("%d\n", lc::binomial(10, 5));
  202.     printf("%d\n", lc::binomial(1996, 998));
  203.     lc::MOD = 10007;
  204.     printf("%d\n", lc::binomial2(10, 5));
  205.     printf("%d\n", lc::binomial2(1996, 998));
  206.     return 0;
  207. }
  208.  
  209.  
  210. /// maximum divisors
  211. unsigned long long n, res, idx;
  212. int p, primes[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71};
  213. unsigned long long mul(unsigned long long a, unsigned long long b){
  214.     unsigned long long res = 0;
  215.     while (b){
  216.         if (b & 1LL) res = (res + a);
  217.         if (res > n) return 0;
  218.         a = (a << 1LL);
  219.         b >>= 1LL;
  220.     }
  221.     return res;
  222. }
  223. void backtrack(int i, int lim, unsigned long long val, unsigned long long r){
  224.     if ((r > res) || (r == res && val < idx)) res = r, idx = val;
  225.     if (i == p) return;
  226.     int d;
  227.     unsigned long long x = val;
  228.     for (d = 1; d <= lim; d++){
  229.         x = mul(x, primes[i]);
  230.         if (x == 0) return;
  231.         backtrack(i + 1, d, x, r * (d + 1));
  232.     }
  233. }
  234. int main(){
  235.     /* Tested for n <= 10^18 */
  236.     p = sizeof(primes) / sizeof(int);
  237.     while (scanf("%llu", &n) != EOF){
  238.         res = 0;
  239.         backtrack(0, 100, 1, 1);
  240.         printf("%llu = %llu\n", idx, res);
  241.     }
  242.     return 0;
  243. }
  244.  
  245.  
  246.  
  247. /// Miller Rabin
  248. #define MAX 1000010
  249. namespace prm{ /// hash = 130793
  250.     bitset <MAX> flag;
  251.     long double op = 0.0;
  252.     int p = 0, prime[78777];
  253.     const int base[] = {2, 325, 9375, 28178, 450775, 9780504, 1795265022};
  254.     void Sieve(){
  255.         int i, j, x;
  256.         for (i = 3; i < MAX; i += 2) flag[i] = true;
  257.         for (i = 3, flag[2] = true; (i * i) < MAX; i += 2){
  258.             if (flag[i]){
  259.                 for (j = (i * i), x = i << 1; j < MAX; j += x){
  260.                     flag[j] = false;
  261.                 }
  262.             }
  263.         }
  264.         for (i = 2; i < MAX; i++){
  265.             if (flag[i]) prime[p++] = i;
  266.         }
  267.     }
  268.     void init(){
  269.         if (!flag[2]) Sieve();
  270.     }
  271.     inline long long mul(long long a, long long b, long long MOD){
  272.        if ((MOD < 3037000500LL)) return ((a * b) % MOD);
  273.        long double res = a;
  274.        res *= b;
  275.        long long c = (long long)(res * op);
  276.        a *= b;
  277.        a -= c * MOD;
  278.        if (a >= MOD) a -= MOD;
  279.        if (a < 0) a += MOD;
  280.        return a;
  281.     }
  282.     inline long long expo(long long x, long long n, long long m){
  283.         long long res = 1;
  284.         while (n){
  285.             if (n & 1) res = mul(res, x, m);
  286.             x = mul(x, x, m);
  287.             n >>= 1;
  288.         }
  289.         return (res % m);
  290.     }
  291.     inline bool miller_rabin(long long p){
  292.         if (p < MAX) return flag[p];
  293.         if ((p + 1) & 1) return false;
  294.         for (int i = 1; i < 10; i++){ /// basic iterations
  295.             if (!(p % prime[i])) return false;
  296.         }
  297.         long long a, m, x, s = p - 1, y = p - 1;
  298.         op = (long double)1 / p, s = s >> __builtin_ctzll(s);
  299.         for (int i = 0; i < 7; i++) {
  300.             x = s, a = (base[i] % y) + 1;
  301.             m = expo(a, x, p);
  302.             while ((x != y) && (m != 1) && (m != y)) m = mul(m, m, p), x <<= 1;
  303.             if ((m != y) && !(x & 1)) return false;
  304.         }
  305.         return true;
  306.     }
  307.     inline long long countdiv(long long n){
  308.         int i, j, c;
  309.         long long x, res = 1;
  310.         for (i = 0; i < p; i++){
  311.             x = prime[i];
  312.             if ((x * x * x) > n) break;
  313.             c = 1;
  314.             while (!(n % x)) c++, n /= x;
  315.             res *= c;
  316.         }
  317.         if (miller_rabin(n)) res <<= 1;
  318.         else if (n > 1) {
  319.             x = sqrt((long double)0.95 + n); /// may be change to sqrtl() ?
  320.             if ((x * x) == n && miller_rabin(x)) res *= 3;
  321.             else res <<= 2;
  322.         }
  323.         return res;
  324.     }
  325. }
  326. int main(){
  327.     prm::init();
  328. }
  329.  
  330.  
  331.  
  332. /// mobius function
  333. #define LEN 666666
  334. #define MAX 10000010
  335. int len = 0, prime[LEN];
  336. char mu[MAX] = {0}, flag[MAX] = {0};
  337. /// mu[1] = 1, mu[n] = 0 if n has a squared prime factor,
  338. /// mu[n] = 1 if n is square-free with even number of prime factors
  339. /// mu[n] = -1 if n is square-free with odd number of prime factors
  340. void Mobius(){
  341.     mu[1] = 1;
  342.     int i, j, k;
  343.     for (i = 2; i < MAX; i++){
  344.         if (!flag[i]) mu[i] = -1, prime[len++] = i;
  345.         for (j = 0; j < len && (k = i * prime[j]) < MAX; j++){
  346.             flag[k] = true;
  347.             if (!(i % prime[j])){
  348.                 mu[k] = 0;
  349.                 break;
  350.             }
  351.             else mu[k] -= mu[i];
  352.         }
  353.     }
  354. }
  355. void Mobius(){ /// Simplified NlogN version
  356.     int i, j;
  357.     mu[1] = 1;
  358.     for (i = 1; i < MAX; i++){
  359.         for (j = i + i; j < MAX; j += i){
  360.             mu[j] -= mu[i];
  361.         }
  362.     }
  363. }
  364. void Mobius(){ /// Simplified version optimized
  365.     int i, j;
  366.     mu[1] = 1;
  367.     for (i = 1; i < MAX; i++){
  368.         if (mu[i]){
  369.             for (j = i + i; j < MAX; j += i){
  370.                 mu[j] -= mu[i];
  371.             }
  372.         }
  373.     }
  374. }
  375. int main(){
  376.     Mobius();
  377. }
  378.  
  379.  
  380.  
  381. /// phi
  382. #define MAX 4000010
  383. int phi[MAX];
  384. void Totient(){
  385.     clr(phi);
  386.     int i, j, x;
  387.     for (i = 1; i < MAX; i++){
  388.         phi[i] += i;
  389.         for (j = (i << 1); j < MAX; j += i){
  390.             phi[j] -= phi[i];
  391.         }
  392.     }
  393. }
  394. int main(){
  395.     Totient();
  396. }
  397.  
  398.  
  399.  
  400. /// Pick's Theorem
  401. struct Point{
  402.     long long x, y;
  403.     Point(){}
  404.     Point(long long x, long long y) : x(x), y(y){}
  405. };
  406. /// twice the area of polygon
  407. long long double_area(Point poly[], int n){
  408.     long long res = 0;
  409.     for (int i = 0, j = n - 1; i < n; j = i++){
  410.         res += ((poly[j].x + poly[i].x) * (poly[j].y - poly[i].y));
  411.     }
  412.     return abs(res);
  413. }
  414. /// number of lattice points strictly on polygon border
  415. long long on_border(Point poly[], int n){
  416.     long long res = 0;
  417.     for (int i = 0, j = n - 1; i < n; j = i++){
  418.         res += __gcd(abs(poly[i].x - poly[j].x), abs(poly[i].y - poly[j].y));
  419.     }
  420.     return res;
  421. }
  422. /// number of lattice points strictly inside polygon
  423. long long interior(Point poly[], int n){
  424.     long long res = 2 + double_area(poly, n) - on_border(poly, n);
  425.     return res / 2;
  426. }
  427. int main(){
  428.     Point ar[10];
  429.     ar[0] = Point(0, 0);
  430.     ar[1] = Point(3, 0);
  431.     ar[2] = Point(3, 3);
  432.     ar[3] = Point(0, 3);
  433.     dbg(on_border(ar, 4)); /// 12
  434.     dbg(interior(ar, 4)); /// 4
  435.     return 0;
  436. }
  437.  
  438.  
  439.  
  440. /// Pollard Rho
  441. const long long LIM = LLONG_MAX;
  442. long long mul(long long a, long long b, long long m){
  443.     long long x, res;
  444.     if (a < b) swap(a, b);
  445.     if (!b) return 0;
  446.     if (a < (LIM / b)) return ((a * b) % m);
  447.     res = 0, x = (a % m);
  448.     while (b){
  449.         if (b & 1){
  450.             res = res + x;
  451.             if (res >= m) res -= m;
  452.         }
  453.         b >>= 1;
  454.         x <<= 1;
  455.         if (x >= m) x -= m;
  456.     }
  457.     return res;
  458. }
  459. long long expo(long long x, long long n, long long m){
  460.     long long res = 1;
  461.  
  462.     while (n){
  463.         if (n & 1) res = mul(res, x, m);
  464.         x = mul(x, x, m);
  465.         n >>= 1;
  466.     }
  467.     return (res % m);
  468. }
  469. const int small_primes[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 51, 53, 59, 61, 67, 71};
  470. bool miller_rabin(long long p, int lim){
  471.     long long a, s, m, x, y;
  472.     s = p - 1, y = p - 1;
  473.     while (!(s & 1)) s >>= 1;
  474.     while (lim--){
  475.         x = s;
  476.         a = (rand() % y) + 1;
  477.         m = expo(a, x, p);
  478.         while ((x != y) && (m != 1) && (m != y)){
  479.             m = mul(m, m, p);
  480.             x <<= 1;
  481.         }
  482.         if ((m != y) && !(x & 1)) return false;
  483.     }
  484.     return true;
  485. }
  486. void brent_pollard_rho(uint64_t n, vector <uint64_t> &v){
  487.     if (miller_rabin(n, 10)){
  488.         v.push_back(n);
  489.         return;
  490.     }
  491.     uint64_t a, g, x, y;
  492.     y = 1;
  493.     a = rand() % n;
  494.     x = rand() % n;
  495.     for (int i = 0; ((i * i) >> 1) < n; i++){
  496.         x = mul(x, x, n);
  497.         x += a;
  498.         if (x < a) x += (ULLONG_MAX - n) + 1;
  499.         x %= n;
  500.         g = __gcd(n, y - x);
  501.         if ((g != 1) && (g != n)){
  502.             n /= g;
  503.             brent_pollard_rho(g, v);
  504.             if (n != g) brent_pollard_rho(n, v);
  505.             else if (miller_rabin(n, 10)) v.push_back(n);
  506.             return;
  507.         }
  508.         if (!(i & (i - 1))) y = x;
  509.     }
  510.     brent_pollard_rho(n, v);
  511. }
  512. void factorize(uint64_t n, vector <uint64_t> &v){
  513.     srand(time(0));
  514.     int i, j, x;
  515.     for (i = 0; i < 21; i++){
  516.         x = small_primes[i];
  517.         while ((n % x) == 0){
  518.             n /= x;
  519.             v.push_back(x);
  520.         }
  521.     }
  522.     if (n > 1) brent_pollard_rho(n, v);
  523.     sort(v.begin(), v.end());
  524. }
  525. int main(){
  526.     int i, t;
  527.     uint64_t n, x;
  528.     cin >> t;
  529.     while (t--){
  530.         cin >> n;
  531.         vector <uint64_t> v;
  532.         factorize(n, v);
  533.         sort(v.begin(), v.end());
  534.         v.push_back(-1);
  535.         int len = v.size();
  536.         int c = 0, counter = 0;
  537.         printf("%llu = ", n);
  538.         for (i = 0; (i + 1) < len; i++){
  539.             if (v[i] == v[i + 1]) counter++;
  540.             else{
  541.                 if (c) printf(" * ");
  542.                 if (counter) printf("%llu^%d", v[i], ++counter);
  543.                 else printf("%llu", v[i]);
  544.                 c++, counter = 0;
  545.             }
  546.         }
  547.         puts("");
  548.     }
  549.     return 0;
  550. }
  551.  
  552.  
  553. /// Prime Counting Functions
  554. #define chkbit(ar, i) (((ar[(i) >> 6]) & (1 << (((i) >> 1) & 31))))
  555. #define setbit(ar, i) (((ar[(i) >> 6]) |= (1 << (((i) >> 1) & 31))))
  556. #define isprime(x) (( (x) && ((x)&1) && (!chkbit(ar, (x)))) || ((x) == 2))
  557. namespace pcf{
  558.     ///   Prime-Counting Function
  559.     ///   initialize once by calling init()
  560.     ///   Legendre(m) and Lehmer(m) returns the number of primes less than or equal to m
  561.     #define MAXN 1000010
  562.     #define MAX_PRIMES 1000010
  563.     #define PHI_M 10010
  564.     #define PHI_N 48
  565.     unsigned int ar[(MAXN >> 6) + 5] = {0};
  566.     int len = 0, primes[MAX_PRIMES], counter[MAXN], phi_dp[PHI_N][PHI_M];
  567.     void Sieve(int n, unsigned int* ar, int* primes, int& len){
  568.         n++;
  569.         setbit(ar, 0), setbit(ar, 1);
  570.         int i, j, k, lim = sqrt(n) + 1;
  571.         for (i = 3; i < lim; i++, i++){
  572.             if (!chkbit(ar, i)){
  573.                 k = i << 1;
  574.                 for (j = (i * i); j < n; j += k) setbit(ar, j);
  575.             }
  576.         }
  577.         for (i = 1; i < n; i++){
  578.             if (isprime(i)) primes[len++] = i;
  579.             counter[i] = len;
  580.         }
  581.     }
  582.     void Sieve(int n){
  583.         Sieve(n, ar, primes, len);
  584.     }
  585.     void init(){
  586.         Sieve(MAXN - 1);
  587.         int m, n, res;
  588.         for (n = 0; n < PHI_N; n++){
  589.             for (m = 0; m < PHI_M; m++){
  590.                 if (!n) res = m;
  591.                 else res = phi_dp[n - 1][m] - phi_dp[n - 1][m / primes[n - 1]];
  592.                 phi_dp[n][m] = res;
  593.             }
  594.         }
  595.     }
  596.     int phi(int m, int n){
  597.         if (m < PHI_M && n < PHI_N) return phi_dp[n][m];
  598.         if (n == 1) return ((++m) >> 1);
  599.         if (primes[n - 1] >= m) return 1;
  600.         return phi(m, n - 1) - phi(m / primes[n - 1], n - 1);
  601.     }
  602.     int Legendre(int m){
  603.         if (m < MAXN) return counter[m];
  604.         int lim = sqrt(m) + 1;
  605.         int n = upper_bound(primes, primes + len, lim) - primes;
  606.         return phi(m, n) + (n - 1);
  607.     }
  608.     int Lehmer(int m){
  609.         if (m < MAXN) return counter[m];
  610.         int i, j, a, b, c, w, lim, res;
  611.         b = sqrt(m), c = Lehmer(cbrt(m)), a = Lehmer(sqrt(b)), b = Lehmer(b);
  612.         res = phi(m, a) + (((b + a - 2) * (b - a + 1)) >> 1);
  613.         for (i = a; i < b; i++){
  614.             w = m / primes[i];
  615.             lim = Lehmer(sqrt(w)), res -= Lehmer(w);
  616.             if (i <= c){
  617.                 for (j = i; j < lim; j++){
  618.                     res += j;
  619.                     res -= Lehmer(w / primes[j]);
  620.                 }
  621.             }
  622.         }
  623.         return res;
  624.     }
  625. }
  626. int main(){
  627.     pcf::init();
  628.     return 0;
  629. }
  630.  
  631.  
  632.  
  633. /// Segmented Sieve
  634. #define MAX 1000010
  635. #define BASE_SQR 216
  636. #define BASE_LEN 10010
  637. #define BASE_MAX 46656
  638. #define chkbit(ar, i) (((ar[(i) >> 6]) & (1 << (((i) >> 1) & 31))))
  639. #define setbit(ar, i) (((ar[(i) >> 6]) |= (1 << (((i) >> 1) & 31))))
  640. int p, primes[BASE_LEN];
  641. unsigned int base[(BASE_MAX >> 6) + 5], isprime[(MAX >> 6) + 5];
  642. void Sieve(){
  643.     clr(base);
  644.     int i, j, k;
  645.     for (i = 3; i < BASE_SQR; i++, i++){
  646.         if (!chkbit(base, i)){
  647.             k = i << 1;
  648.             for (j = (i * i); j < BASE_MAX; j += k){
  649.                 setbit(base, j);
  650.             }
  651.         }
  652.     }
  653.     p = 0;
  654.     for (i = 3; i < BASE_MAX; i++, i++){
  655.         if (!chkbit(base, i)){
  656.             primes[p++] = i;
  657.         }
  658.     }
  659. }
  660. int SegmentedSieve(long long a, long long b){
  661.     long long j, k, x;
  662.     int i, d, counter = 0;
  663.     if (a <= 2 && 2 <= b) counter = 1; /// 2 is counted separately if in range
  664.     if (!(a & 1)) a++;
  665.     if (!(b & 1)) b--;
  666.     if (a > b) return counter;
  667.     clr(isprime);
  668.     for (i = 0; i < p; i++){
  669.         x = primes[i];
  670.         if ((x * x) > b) break;
  671.         k = x << 1;
  672.         j = x * ((a + x - 1) / x);
  673.         if (!(j & 1)) j += x;
  674.         else if (j == x) j += k;
  675.         while (j <= b){
  676.             setbit(isprime, j - a);
  677.             j += k;
  678.         }
  679.     }
  680.     /// Other primes in the range except 2 are added here
  681.     d = (b - a + 1);
  682.     for (i = 0; i < d; i++, i++){
  683.         if (!chkbit(isprime, i) && (a + i) != 1) counter++;
  684.     }
  685.     return counter;
  686. }
  687. int main(){
  688.     Sieve();
  689.     int T = 0, t, i, j, a, b;
  690.     scanf("%d", &t);
  691.     while (t--){
  692.         scanf("%d %d", &a, &b);
  693.         printf("Case %d: %d\n", ++T, SegmentedSieve(a, b));
  694.     }
  695.     return 0;
  696. }
  697.  
  698.  
  699.  
  700. /// simplex
  701. #define MAXC 1010
  702. #define MAXV 1010
  703. #define EPS 1e-13
  704. #define MINIMIZE -1
  705. #define MAXIMIZE +1
  706. #define LESSEQ -1
  707. #define EQUAL 0
  708. #define GREATEQ 1
  709. #define INFEASIBLE -1
  710. #define UNBOUNDED 666
  711. namespace lp{
  712.     long double val[MAXV], ar[MAXC][MAXV];
  713.     int m, n, solution_flag, minmax_flag, basis[MAXC], index[MAXV];
  714.     inline void init(int nvars, long double f[], int flag){
  715.         solution_flag = 0;
  716.         ar[0][nvars] = 0.0;
  717.         m = 0, n = nvars, minmax_flag = flag;
  718.         for (int i = 0; i < n; i++){
  719.             ar[0][i] = f[i] * minmax_flag;
  720.         }
  721.     }
  722.     inline void add_constraint(long double C[], long double lim, int cmp){
  723.         m++, cmp *= -1;
  724.         if (cmp == 0){
  725.             for (int i = 0; i < n; i++) ar[m][i] = C[i];
  726.             ar[m++][n] = lim;
  727.             for (int i = 0; i < n; i++) ar[m][i] = -C[i];
  728.             ar[m][n] = -lim;
  729.         }
  730.         else{
  731.             for (int i = 0; i < n; i++) ar[m][i] = C[i] * cmp;
  732.             ar[m][n] = lim * cmp;
  733.         }
  734.     }
  735.     inline void init(){
  736.         for (int i = 0; i <= m; i++) basis[i] = -i;
  737.         for (int j = 0; j <= n; j++){
  738.             ar[0][j] = -ar[0][j], index[j] = j, val[j] = 0;
  739.         }
  740.     }
  741.     inline void pivot(int m, int n, int a, int b){
  742.         for (int i = 0; i <= m; i++){
  743.             if (i != a){
  744.                 for (int j = 0; j <= n; j++){
  745.                     if (j != b){
  746.                         ar[i][j] -= (ar[i][b] * ar[a][j]) / ar[a][b];
  747.                     }
  748.                 }
  749.             }
  750.         }
  751.         for (int j = 0; j <= n; j++){
  752.             if (j != b) ar[a][j] /= ar[a][b];
  753.         }
  754.         for (int i = 0; i <= m; i++){
  755.             if (i != a) ar[i][b] = -ar[i][b] / ar[a][b];
  756.         }
  757.         ar[a][b] = 1.0 / ar[a][b];
  758.         swap(basis[a], index[b]);
  759.     }
  760.     inline long double solve(){
  761.         init();
  762.         int i, j, k, l;
  763.         for (; ;){
  764.             for (i = 1, k = 1; i <= m; i++){
  765.                 if ((ar[i][n] < ar[k][n]) || (ar[i][n] == ar[k][n] && basis[i] < basis[k] && (rand() & 1))) k = i;
  766.             }
  767.             if (ar[k][n] >= -EPS) break;
  768.  
  769.             for (j = 0, l = 0; j < n; j++){
  770.                 if ((ar[k][j] < (ar[k][l] - EPS)) || (ar[k][j] < (ar[k][l] - EPS) && index[i] < index[j] && (rand() & 1))){
  771.                     l = j;
  772.                 }
  773.             }
  774.             if (ar[k][l] >= -EPS){
  775.                 solution_flag = INFEASIBLE;
  776.                 return -1.0;
  777.             }
  778.             pivot(m, n, k, l);
  779.         }
  780.         for (; ;){
  781.             for (j = 0, l = 0; j < n; j++){
  782.                 if ((ar[0][j] < ar[0][l]) || (ar[0][j] == ar[0][l] && index[j] < index[l] && (rand() & 1))) l = j;
  783.             }
  784.             if (ar[0][l] > -EPS) break;
  785.             for (i = 1, k = 0; i <= m; i++){
  786.                 if (ar[i][l] > EPS && (!k || ar[i][n] / ar[i][l] < ar[k][n] / ar[k][l] - EPS || (ar[i][n] / ar[i][l] < ar[k][n] / ar[k][l] + EPS && basis[i] < basis[k]))){
  787.                     k = i;
  788.                 }
  789.             }
  790.             if (ar[k][l] <= EPS){
  791.                 solution_flag = UNBOUNDED;
  792.                 return -666.0;
  793.             }
  794.             pivot(m, n, k, l);
  795.         }
  796.         for (i = 1; i <= m; i++){
  797.             if (basis[i] >= 0) val[basis[i]] = ar[i][n];
  798.         }
  799.         solution_flag = 1;
  800.         return (ar[0][n] * minmax_flag);
  801.     }
  802. }
  803.  
  804.  
  805. /// Thomas algorithm to solve tri-digonal system of equations in O(n)
  806. /// Equation of the form: (x_prev * l) + (x_cur * p) + (x_next * r) = rhs
  807. struct equation{
  808.     long double l, p, r, rhs;
  809.     equation(){}
  810.     equation(long double l, long double p, long double r, long double rhs = 0.0):
  811.         l(l), p(p), r(r), rhs(rhs){}
  812. };
  813. vector <long double> thomas_algorithm(int n, vector <struct equation> ar){
  814.     ar[0].r = ar[0].r / ar[0].p;
  815.     ar[0].rhs = ar[0].rhs / ar[0].p;
  816.     for (int i = 1; i < n; i++){
  817.         long double v = 1.0 / (ar[i].p - ar[i].l * ar[i - 1].r);
  818.         ar[i].r = ar[i].r * v;
  819.         ar[i].rhs = (ar[i].rhs - ar[i].l * ar[i - 1].rhs) * v;
  820.     }
  821.     for (int i = n - 2; i >= 0; i--) ar[i].rhs = ar[i].rhs - ar[i].r * ar[i + 1].rhs;
  822.     vector <long double> res;
  823.     for (int i = 0; i < n; i++) res.push_back(ar[i].rhs);
  824.     return res;
  825. }
  826.  
  827.  
  828.  
  829. /// Karatsuba
  830. #define MAX 131072 /// Must be a power of 2
  831. #define MOD 1000000007
  832. unsigned long long temp[128];
  833. int ptr = 0, buffer[MAX * 6];
  834. /// n is a power of 2
  835. void karatsuba(int n, int *a, int *b, int *res){ /// hash = 829512
  836.     int i, j, h;
  837.     if (n < 17){ /// Reduce recursive calls by setting a threshold
  838.         for (i = 0; i < (n + n); i++) temp[i] = 0;
  839.         for (i = 0; i < n; i++){
  840.             if (a[i]){
  841.                 for (j = 0; j < n; j++){
  842.                     temp[i + j] += ((long long)a[i] * b[j]);
  843.                 }
  844.             }
  845.         }
  846.         for (i = 0; i < (n + n); i++) res[i] = temp[i] % MOD;
  847.         return;
  848.     }
  849.     h = n >> 1;
  850.     karatsuba(h, a, b, res);
  851.     karatsuba(h, a + h, b + h, res + n);
  852.     int *x = buffer + ptr, *y = buffer + ptr + h, *z = buffer + ptr + h + h;
  853.  
  854.     ptr += (h + h + n);
  855.     for (i = 0; i < h; i++){
  856.         x[i] = a[i] + a[i + h], y[i] = b[i] + b[i + h];
  857.         if (x[i] >= MOD) x[i] -= MOD;
  858.         if (y[i] >= MOD) y[i] -= MOD;
  859.     }
  860.     karatsuba(h, x, y, z);
  861.     for (i = 0; i < n; i++) z[i] -= (res[i] + res[i + n]);
  862.     for (i = 0; i < n; i++){
  863.         res[i + h] = (res[i + h] + z[i]) % MOD;
  864.         if (res[i + h] < 0) res[i + h] += MOD;
  865.     }
  866.     ptr -= (h + h + n);
  867. }
  868. /// multiplies two polynomial a(degree n) and b(degree m) and returns the result modulo MOD in a
  869. /// returns the degree of the multiplied polynomial
  870. /// note that a and b are changed in the process
  871. int mul(int n, int *a, int m, int *b){ /// hash = 903808
  872.     int i, r, c = (n < m ? n : m), d = (n > m ? n : m), *res = buffer + ptr;
  873.     r = 1 << (32 - __builtin_clz(d) - (__builtin_popcount(d) == 1));
  874.     for (i = d; i < r; i++) a[i] = b[i] = 0;
  875.     for (i = c; i < d && n < m; i++) a[i] = 0;
  876.     for (i = c; i < d && m < n; i++) b[i] = 0;
  877.  
  878.     ptr += (r << 1), karatsuba(r, a, b, res), ptr -= (r << 1);
  879.     for (i = 0; i < (r << 1); i++) a[i] = res[i];
  880.     return (n + m - 1);
  881. }
  882. int a[MAX * 2], b[MAX * 2];
  883. int main(){
  884.     int i, j, k, n = MAX - 10;
  885.     for (i = 0; i < n; i++) a[i] = ran(1, 1000000000);
  886.     for (i = 0; i < n; i++) b[i] = ran(1, 991929183);
  887.     clock_t start = clock();
  888.     mul(n, a, n, b);
  889.     dbg(a[n / 2]);
  890.     for (i = 0; i < (n << 1); i++){
  891.         if (a[i] < 0) puts("YO");
  892.     }
  893.     printf("%0.5f\n", (clock() - start) / (1.0 * CLOCKS_PER_SEC));
  894.     return 0;
  895. }
  896.  
  897.  
  898. /// Integral Determinant
  899. /// Finds the determinant of a square matrix
  900. /// Returns 0 if the matrix is singular or degenerate (hence no determinant exists)
  901. /// Absolute value of final answer should be < MOD / 2
  902. #define MAX 1010
  903. const long long MOD = 4517409488245517117LL;
  904. const long double OP = (long double)1 / 4517409488245517117LL;
  905. long long mul(long long a, long long b){
  906.    long double res = a;
  907.    res *= b;
  908.    long long c = (long long)(res * OP);
  909.    a *= b;
  910.    a -= c * MOD;
  911.    if (a >= MOD) a -= MOD;
  912.    if (a < 0) a += MOD;
  913.    return a;
  914. }
  915. long long expo(long long x, long long n){
  916.     long long res = 1;
  917.     while (n){
  918.         if (n & 1) res = mul(res, x);
  919.         x = mul(x, x);
  920.         n >>= 1;
  921.     }
  922.     return res;
  923. }
  924. int gauss(int n, long long ar[MAX][MAX]){
  925.     long long x, y;
  926.     int i, j, k, l, p, counter = 0;
  927.     for (i = 0; i < n; i++){
  928.         for (p = i, j = i + 1; j < n && !ar[p][i]; j++){
  929.             p = j;
  930.         }
  931.         if (!ar[p][i]) return -1;
  932.         for (j = i; j < n; j++){
  933.             x = ar[p][j], ar[p][j] = ar[i][j], ar[i][j] = x;
  934.         }
  935.         if (p != i) counter++;
  936.         for (j = i + 1; j < n; j++){
  937.             x = expo(ar[i][i], MOD - 2);
  938.             x = mul(x, MOD - ar[j][i]);
  939.             for (k = i; k < n; k++){
  940.                 ar[j][k] = ar[j][k] + mul(x, ar[i][k]);
  941.                 if (ar[j][k] >= MOD) ar[j][k] -= MOD;
  942.             }
  943.         }
  944.     }
  945.     return counter;
  946. }
  947. long long determinant(int n, long long ar[MAX][MAX]){
  948.     int i, j, free;
  949.     long long res = 1;
  950.     for (i = 0; i < n; i++){
  951.         for (j = 0; j < n; j++){
  952.             if (ar[i][j] < 0) ar[i][j] += MOD;
  953.         }
  954.     }
  955.     free = gauss(n, ar);
  956.     if (free == -1) return 0; /// Determinant is 0 so matrix is not invertible, singular or degenerate matrix
  957.     for (i = 0; i < n; i++) res = mul(res, ar[i][i]);
  958.     if (free & 1) res = MOD - res;
  959.     if ((MOD - res) < res) res -= MOD; /// Determinant can be negative so if determinant is more close to MOD than 0, make it negative
  960.     return res;
  961. }
  962. int n;
  963. long long ar[MAX][MAX];
  964. int main(){
  965.     int t, i, j, k, l;
  966.     while (scanf("%d", &n) != EOF){
  967.         if (n == 0) break;
  968.         for (i = 0; i < n; i++){
  969.             for (j = 0; j < n; j++){
  970.                 scanf("%lld", &ar[i][j]);
  971.             }
  972.         }
  973.         printf("%lld\n", determinant(n, ar));
  974.     }
  975.     return 0;
  976. }
  977.  
  978.  
  979.  
  980. /// Gaussian Elimination in Modular Field
  981. #define MAXROW 1010
  982. #define MAXCOL 1010
  983. int expo(int a, int b, int MOD){
  984.     int res = 1;
  985.     while (b){
  986.         if (b & 1) res = (long long)res * a % MOD;
  987.         a = (long long)a * a % MOD;
  988.         b >>= 1;
  989.     }
  990.     return res;
  991. }
  992. /// Gaussian elimination in field MOD (MOD should be a prime)
  993. int gauss(int n, int m, int MOD, int ar[MAXROW][MAXCOL], vector<int>& res){
  994.     res.assign(m, 0);
  995.     vector <int> pos(m, -1);
  996.     int i, j, k, l, p, d, free_var = 0;
  997.     const long long MODSQ = (long long)MOD * MOD;
  998.     for (j = 0, i = 0; j < m && i < n; j++){
  999.         for (k = i, p = i; k < n; k++){
  1000.             if (ar[k][j] > ar[p][j]) p = k;
  1001.         }
  1002.         if (ar[p][j]){
  1003.             pos[j] = i;
  1004.             for (l = j; l <= m; l++) swap(ar[p][l], ar[i][l]);
  1005.             d = expo(ar[i][j], MOD - 2, MOD);
  1006.             for (k = 0; k < n && d; k++){
  1007.                 if (k != i && ar[k][j]){
  1008.                     int x = ((long long)ar[k][j] * d) % MOD;
  1009.                     for (l = j; l <= m && x; l++){
  1010.                         if (ar[i][l]) ar[k][l] = (MODSQ + ar[k][l] - ((long long)ar[i][l] * x)) % MOD;
  1011.                     }
  1012.                 }
  1013.             }
  1014.             i++;
  1015.         }
  1016.     }
  1017.     for (i = 0; i < m; i++){
  1018.         if (pos[i] == -1) free_var++;
  1019.         else res[i] = ((long long)ar[pos[i]][m] * expo(ar[pos[i]][i], MOD - 2, MOD)) % MOD;
  1020.     }
  1021.     for (i = 0; i < n; i++) {
  1022.         long long val = 0;
  1023.         for (j = 0; j < m; j++) val = (val + ((long long)res[j] * ar[i][j])) % MOD;
  1024.         if (val != ar[i][m]) return -1;
  1025.     }
  1026.     return free_var;
  1027. }
  1028.  
  1029.  
  1030.  
  1031. /// Gaussian Elimination Extended
  1032. #define EPS 1e-9
  1033. #define MAXROW 512
  1034. #define MAXCOL 512
  1035.  
  1036. /***
  1037.  
  1038. Gauss-Jordan Elimination
  1039.  
  1040. n = number of linear equations
  1041. m = number of variables
  1042. ar[i][m] = right-hand side value of constants
  1043.  
  1044. For instance, the system of linear equations becomes:
  1045.  
  1046. 2x + y -z = 8      ----->  (i)
  1047. -3x -y + 2z = -11  ----->  (ii)
  1048. -2x + y + 2z = -3  ----->  (iii)
  1049.  
  1050. n = 3 (x, y, z), m = 3 (i, ii, iii)
  1051. ar[0] = {2, 1, -1, 8}    ----->  (i)
  1052. ar[1] = {-3, -1, 2, -11} ----->  (ii)
  1053. ar[2] = {-2, 1, 2, -3}   ----->  (iii)
  1054.  
  1055.  
  1056. Returns -1 when there is no solution
  1057. Otherwise returns the number of independent variables (0 for an unique solution)
  1058. Contains a solution in the vector res on successful completion
  1059. Note that the array is modified in the process
  1060.  
  1061. Notes:
  1062. For solving problems on graphs with probability/expectation, make sure the graph
  1063. is connected and a single component. If not, then re-number the vertex and solve
  1064. for each connected component separately.
  1065.  
  1066. ***/
  1067. int gauss(int n, int m, double ar[MAXROW][MAXCOL], vector<double>& res){ /// hash = 835176
  1068.     res.assign(m, 0);
  1069.     vector <int> pos(m, -1);
  1070.     int i, j, k, l, p, free_var = 0;
  1071.     for (j = 0, i = 0; j < m && i < n; j++){
  1072.         for (k = i, p = i; k < n; k++){
  1073.             if (abs(ar[k][j]) > abs(ar[p][j])) p = k;
  1074.         }
  1075.         if (abs(ar[p][j]) > EPS){
  1076.             pos[j] = i;
  1077.             for (l = j; l <= m; l++) swap(ar[p][l], ar[i][l]);
  1078.             for (k = 0; k < n; k++){
  1079.                 if (k != i){
  1080.                     double x = ar[k][j] / ar[i][j];
  1081.                     for (l = j; l <= m; l++) ar[k][l] -= (ar[i][l] * x);
  1082.                 }
  1083.             }
  1084.             i++;
  1085.         }
  1086.     }
  1087.     for (i = 0; i < m; i++){
  1088.         if (pos[i] == -1) free_var++;
  1089.         else res[i] = ar[pos[i]][m] / ar[pos[i]][i];
  1090.     }
  1091.     for (i = 0; i < n; i++) {
  1092.         double val = 0.0;
  1093.         for (j = 0; j < m; j++) val += (res[j] * ar[i][j]);
  1094.         if (abs(val - ar[i][m]) > EPS) return -1;
  1095.     }
  1096.     return free_var;
  1097. }
  1098.  
  1099.  
  1100.  
  1101. /// FFT extended
  1102. #define MAXN 1048576 /// 2 * MAX at least
  1103. /// Change long double to double if not required
  1104. namespace fft{
  1105.     int len, last = -1, step = 0, rev[MAXN];
  1106.     long long C[MAXN], D[MAXN], P[MAXN], Q[MAXN];
  1107.     struct complx{
  1108.         long double real, img;
  1109.         inline complx(){
  1110.             real = img = 0.0;
  1111.         }
  1112.         inline complx conjugate(){
  1113.             return complx(real, -img);
  1114.         }
  1115.         inline complx(long double x){
  1116.             real = x, img = 0.0;
  1117.         }
  1118.         inline complx(long double x, long double y){
  1119.             real = x, img = y;
  1120.         }
  1121.         inline complx operator + (complx other){
  1122.             return complx(real + other.real, img + other.img);
  1123.         }
  1124.         inline complx operator - (complx other){
  1125.             return complx(real - other.real, img - other.img);
  1126.         }
  1127.         inline complx operator * (complx other){
  1128.             return complx((real * other.real) - (img * other.img), (real * other.img) + (img * other.real));
  1129.         }
  1130.     } u[MAXN], v[MAXN], f[MAXN], g[MAXN], dp[MAXN], inv[MAXN];
  1131.     /// Pre-process roots, inverse roots and fft leaf index
  1132.     void build(int& a, long long* A, int& b, long long* B){
  1133.         while (a > 1 && A[a - 1] == 0) a--;
  1134.         while (b > 1 && B[b - 1] == 0) b--;
  1135.         len = 1 << (32 - __builtin_clz(a + b) - (__builtin_popcount(a + b) == 1));
  1136.         for (int i = a; i < len; i++) A[i] = 0;
  1137.         for (int i = b; i < len; i++) B[i] = 0;
  1138.         if (!step++){
  1139.             dp[1] = inv[1] = complx(1);
  1140.             for (int i = 1; (1 << i) < MAXN; i++){
  1141.                 double theta = (2.0 * acos(0.0)) / (1 << i);
  1142.                 complx mul = complx(cos(theta), sin(theta));
  1143.                 complx inv_mul = complx(cos(-theta), sin(-theta));
  1144.  
  1145.                 int lim = 1 << i;
  1146.                 for (int j = lim >> 1; j < lim; j++){
  1147.                     dp[2 * j] = dp[j], inv[2 * j] = inv[j];
  1148.                     inv[2 * j + 1] = inv[j] * inv_mul;
  1149.                     dp[2 * j + 1] = dp[j] * mul;
  1150.                 }
  1151.             }
  1152.         }
  1153.         if (last != len){
  1154.             last = len;
  1155.             int bit = (32 - __builtin_clz(len) - (__builtin_popcount(len) == 1));
  1156.             for (int i = 0; i < len; i++) rev[i] = (rev[i >> 1] >> 1) + ((i & 1) << (bit - 1));
  1157.         }
  1158.     }
  1159.     /// Fast Fourier Transformation, iterative divide and conquer
  1160.     void transform(complx *in, complx *out, complx* ar){
  1161.         for (int i = 0; i < len; i++) out[i] = in[rev[i]];
  1162.         for (int k = 1; k < len; k <<= 1){
  1163.             for (int i = 0; i < len; i += (k << 1)){
  1164.                 for (int j = 0; j < k; j++){
  1165.                     complx z = out[i + j + k] * ar[j + k];
  1166.                     out[i + j + k] = out[i + j] - z;
  1167.                     out[i + j] = out[i + j] + z;
  1168.                 }
  1169.             }
  1170.         }
  1171.     }
  1172.     /// Fast Fourier Transformation, iterative divide and conquer unrolled and optimized
  1173.     void transform_unrolled(complx *in, complx *out, complx* ar){
  1174.         for (int i = 0; i < len; i++) out[i] = in[rev[i]];
  1175.         for (int k = 1; k < len; k <<= 1){
  1176.             for (int i = 0; i < len; i += (k << 1)){
  1177.                 complx z, *a = out + i, *b = out + i + k, *c = ar + k;
  1178.                 if (k == 1){
  1179.                     z = (*b) * (*c);
  1180.                     *b = *a - z, *a = *a + z;
  1181.                 }
  1182.                 for (int j = 0; j < k && k > 1; j += 2, a++, b++, c++){
  1183.                     z = (*b) * (*c);
  1184.                     *b = *a - z, *a = *a + z;
  1185.                     a++, b++, c++;
  1186.                     z = (*b) * (*c);
  1187.                     *b = *a - z, *a = *a + z;
  1188.                 }
  1189.             }
  1190.         }
  1191.     }
  1192.     bool equals(int a, long long* A, int b, long long* B){
  1193.         if (a != b) return false;
  1194.         for (a = 0; a < b && A[a] == B[a]; a++){}
  1195.         return (a == b);
  1196.     }
  1197.     /// Square of a polynomial
  1198.     int square(int a, long long* A){
  1199.         build(a, A, a, A);
  1200.         for (int i = 0; i < len; i++) u[i] = complx(A[i], 0);
  1201.         transform_unrolled(u, f, dp);
  1202.         for (int i = 0; i < len; i++) u[i] = f[i] * f[i];
  1203.         transform_unrolled(u, f, inv);
  1204.         for (int i = 0; i < len; i++) A[i] = (f[i].real / (long double)len) + 0.5;
  1205.         return a + a - 1;
  1206.     }
  1207.     /// Multiplies two polynomials A and B and return the coefficients of their product in A
  1208.     /// Function returns degree of the polynomial A * B
  1209.     int multiply(int a, long long* A, int b, long long* B){
  1210.         if (equals(a, A, b, B)) return square(a, A); /// Optimization
  1211.         build(a, A, b, B);
  1212.         for (int i = 0; i < len; i++) u[i] = complx(A[i], B[i]);
  1213.         transform_unrolled(u, f, dp);
  1214.         for (int i = 0; i < len; i++){
  1215.             int j = (len - 1) & (len - i);
  1216.             u[i] = (f[j] * f[j] - f[i].conjugate() * f[i].conjugate()) * complx(0, -0.25 / len);
  1217.         }
  1218.         transform_unrolled(u, f, dp);
  1219.         for (int i = 0; i < len; i++) A[i] = f[i].real + 0.5;
  1220.         return a + b - 1;
  1221.     }
  1222.     /// Modular multiplication
  1223.     int mod_multiply(int a, long long* A, int b, long long* B, int mod){
  1224.         build(a, A, b, B);
  1225.         int flag = equals(a, A, b, B);
  1226.         for (int i = 0; i < len; i++) A[i] %= mod, B[i] %= mod;
  1227.         for (int i = 0; i < len; i++) u[i] = complx(A[i] & 32767, A[i] >> 15);
  1228.         for (int i = 0; i < len; i++) v[i] = complx(B[i] & 32767, B[i] >> 15);
  1229.         transform_unrolled(u, f, dp);
  1230.         for (int i = 0; i < len; i++) g[i] = f[i];
  1231.         if (!flag) transform_unrolled(v, g, dp);
  1232.         for (int i = 0; i < len; i++){
  1233.             int j = (len - 1) & (len - i);
  1234.             complx c1 = f[j].conjugate(), c2 = g[j].conjugate();
  1235.             complx a1 = (f[i] + c1) * complx(0.5, 0);
  1236.             complx a2 = (f[i] - c1) * complx(0, -0.5);
  1237.             complx b1 = (g[i] + c2) * complx(0.5 / len, 0);
  1238.             complx b2 = (g[i] - c2) * complx(0, -0.5 / len);
  1239.             v[j] = a1 * b2 + a2 * b1;
  1240.             u[j] = a1 * b1 + a2 * b2 * complx(0, 1);
  1241.         }
  1242.         transform_unrolled(u, f, dp);
  1243.         transform_unrolled(v, g, dp);
  1244.         long long x, y, z;
  1245.         for (int i = 0; i < len; i++){
  1246.             x = f[i].real + 0.5, y = g[i].real + 0.5, z = f[i].img + 0.5;
  1247.             A[i] = (x + ((y % mod) << 15) + ((z % mod) << 30)) % mod;
  1248.         }
  1249.         return a + b - 1;
  1250.     }
  1251.     /// Multiplies two polynomials where intermediate and final values fits in long long
  1252.     int long_multiply(int a, long long* A, int b, long long* B){
  1253.         int mod1 = 1.5e9;
  1254.         int mod2 = mod1 + 1;
  1255.         for (int i = 0; i < a; i++) C[i] = A[i];
  1256.         for (int i = 0; i < b; i++) D[i] = B[i];
  1257.         mod_multiply(a, A, b, B, mod1);
  1258.         mod_multiply(a, C, b, D, mod2);
  1259.         for (int i = 0; i < len; i++){
  1260.             A[i] = A[i] + (C[i] - A[i] + (long long)mod2) * (long long)mod1 % mod2 * mod1;
  1261.         }
  1262.         return a + b - 1;
  1263.     }
  1264.     int build_convolution(int n, long long* A, long long* B){
  1265.         int i, m, d = 0;
  1266.         for (i = 0; i < n; i++) Q[i] = Q[i + n] = B[i];
  1267.         for (i = 0; i < n; i++) P[i] = A[i], P[i + n] = 0;
  1268.         n *= 2, m = 1 << (32 - __builtin_clz(n) - (__builtin_popcount(n) == 1));
  1269.         for (i = n; i < m; i++) P[i] = Q[i] = 0;
  1270.         return n;
  1271.     }
  1272.     /***
  1273.         Computes the circular convolution of A and B, denoted A * B, in C
  1274.         A and B must be of equal size, if not normalize before calling function
  1275.         Example to demonstrate convolution for n = 5:
  1276.  
  1277.         c0 = a0b0 + a1b4 + a2b3 + a3b2 + a4b1
  1278.         c1 = a0b1 + a1b0 + a2b4 + a3b3 + a4b2
  1279.         ...
  1280.         c4 = a0b4 + a1b3 + a2b2 + a3b1 + a4b0
  1281.  
  1282.  
  1283.         Note: If linear convolution is required, pad with zeros appropriately, as in multiplication
  1284.  
  1285.     ***/
  1286.     /// Returns the convolution of A and B in A
  1287.     void convolution(int n, long long* A, long long* B){
  1288.         int len = build_convolution(n, A, B);
  1289.         multiply(len, P, len, Q);
  1290.         for (int i = 0; i < n; i++) A[i] = P[i + n];
  1291.     }
  1292.     /// Modular convolution
  1293.     void mod_convolution(int n, long long* A, long long* B, int mod){
  1294.         int len = build_convolution(n, A, B);
  1295.         mod_multiply(len, P, len, Q, mod);
  1296.         for (int i = 0; i < n; i++) A[i] = P[i + n];
  1297.     }
  1298.     /// Convolution in long long
  1299.     void long_convolution(int n, long long* A, long long* B){
  1300.         int len = build_convolution(n, A, B);
  1301.         long_multiply(len, P, len, Q);
  1302.         for (int i = 0; i < n; i++) A[i] = P[i + n];
  1303.     }
  1304.     /// Hamming distance vector of B with every substring of length |pattern| in str
  1305.     /// str and pattern consists of only '1' and '0'
  1306.     /// str = "01111000010011111111110010001101000100011110101111"
  1307.     /// pattern = "1001101001101110101101000"
  1308.     /// Sum of values in hamming distance vector = 321
  1309.     vector <int> hamming_distance(const char* str, const char* pattern){
  1310.         int n = strlen(str), m = strlen(pattern);
  1311.         for (int i = 0; i < n; i++) P[i] = Q[i] = 0;
  1312.         for (int i = 0; i < n; i++) P[i] = str[i] == '1' ? 1 : -1;
  1313.         for (int i = 0, j = m - 1; j >= 0; i++, j--) Q[i] = pattern[j] == '1' ? 1 : -1;
  1314.         vector <int> res;
  1315.         fft::multiply(n, P, m, Q);
  1316.         for (int i = 0; (i + m) <= n; i++){
  1317.             res.push_back(m - ((P[i + m - 1] + m) >> 1));
  1318.         }
  1319.         return res;
  1320.     }
  1321. }
  1322.  
  1323.  
  1324.  
  1325. /// Fast Integer Cube and Square Root
  1326. unsigned int fast_sqrt(unsigned int n){
  1327.     unsigned int c, g;
  1328.     c = g = 0x8000;
  1329.     for (; ;){
  1330.         if ((g * g) > n) g ^= c;
  1331.         c >>= 1;
  1332.         if (!c) return g;
  1333.         g |= c;
  1334.     }
  1335. }
  1336. int fast_cbrt(int n){
  1337.     int x, r = 30, res = 0;
  1338.     for (; r >= 0; r -= 3){
  1339.         res <<= 1;
  1340.         x = (3 * res * (res + 1)) + 1;
  1341.         if ((n >> r) >= x){
  1342.             res++;
  1343.             n -= (x << r);
  1344.         }
  1345.     }
  1346.     return res;
  1347. }
  1348. unsigned long long fast_sqrt(unsigned long long n){
  1349.     unsigned long long c, g;
  1350.     c = g = 0x80000000;
  1351.     for (; ;){
  1352.         if ((g * g) > n) g ^= c;
  1353.         c >>= 1;
  1354.         if (!c) return g;
  1355.         g |= c;
  1356.     }
  1357. }
  1358. unsigned long long fast_cbrt(unsigned long long n){
  1359.     int r = 63;
  1360.     unsigned long long x, res = 0;
  1361.     for (; r >= 0; r -= 3){
  1362.         res <<= 1;
  1363.         x = (res * (res + 1) * 3) + 1;
  1364.         if ((n >> r) >= x){
  1365.             res++;
  1366.             n -= (x << r);
  1367.         }
  1368.     }
  1369.     return res;
  1370. }
  1371.  
  1372.  
  1373.  
  1374. /// Extended Euclid
  1375. /// Bezout's identity, ax + by = gcd(a,b)
  1376. int extended_gcd(int a, int b, int& x, int& y){
  1377.     if (!b){
  1378.         y = 0, x = 1;
  1379.         return a;
  1380.     }
  1381.     int g = extended_gcd(b, a % b, y, x);
  1382.     y -= ((a / b) * x);
  1383.     return g;
  1384. }
  1385. /// Linear diophantine equation, ax + by = c
  1386. void diophantine(int a, int b, int c, int& x, int& y){
  1387.     int g = extended_gcd(a, b, x, y);
  1388.     assert((c % g) == 0); /// c must be a multiply of g
  1389.     c /= g;
  1390.     x *= c, y *= c;
  1391. }
  1392.  
  1393.  
  1394.  
  1395. /// Eulerian Numbers
  1396. #define MAX 5010
  1397. #define MOD 1000000007
  1398. /***
  1399. Eulerian number A(n,k) is the number of permutations of 1 to n in which exactly k elements are greater than their previous element
  1400. Eulerian triangle for n = 1 to 7 and k = 0 to n - 1 below
  1401.  
  1402. 1
  1403. 1 1
  1404. 1 4 1
  1405. 1 11 11 1
  1406. 1 26 66 26 1
  1407. 1 57 302 302 57 1
  1408. 1 120 1191 2416 1191 120 1
  1409.  
  1410. ***/
  1411. int dp[MAX][MAX];
  1412. void generate(){
  1413.     int n, k;
  1414.     for (n = 0; n < MAX; n++){
  1415.         for (k = 1, dp[n][0] = 1; k <= n; k++){
  1416.             dp[n][k] = ((long long)dp[n - 1][k] * (k + 1) + (long long)dp[n - 1][k - 1] * (n - k)) % MOD;
  1417.         }
  1418.     }
  1419. }
  1420. int main(){
  1421.     int n, k;
  1422.     generate();
  1423.     for (n = 1; n <= 7; n++){
  1424.         for (k = 0; k < n; k++){
  1425.             printf("%d ", dp[n][k]);
  1426.         }
  1427.         puts("");
  1428.     }
  1429.     return 0;
  1430. }
  1431.  
  1432.  
  1433.  
  1434. /// Discrete Log (General)
  1435. int extended_gcd(int a, int b, int& x, int& y){
  1436.     /// Bezout's identity, ax + by = gcd(a,b)
  1437.     if (!b){
  1438.         y = 0, x = 1;
  1439.         return a;
  1440.     }
  1441.     int g = extended_gcd(b, a % b, y, x);
  1442.     y -= ((a / b) * x);
  1443.     return g;
  1444. }
  1445. int discrete_log(int g, int h, int p){ /// hash = 167626
  1446.     /***
  1447.         * returns smallest x such that (g^x) % p = h, -1 if none exists
  1448.         * function returns x, the discrete log of h with respect to g modulo p
  1449.     ***/
  1450.     if (h >= p) return -1;
  1451.     if ((g % p) == 0){
  1452.         if (h == 1) return 0; /// return -1 if strictly positive integer solution is required
  1453.         else return -1;
  1454.     }
  1455.     int i, c, x, y, z, r, m, counter = 0;
  1456.     long long v = 1, d = 1, mul = 1, temp = 1 % p;
  1457.     for (int i = 0; i < 100; i++){
  1458.         if (temp == h) return i;
  1459.         temp = (temp * g) % p;
  1460.     }
  1461.     while ((v = __gcd(g, p)) > 1){
  1462.         if (h % v) return -1;
  1463.         h /= v, p /= v;
  1464.         d = (d * (g / v)) % p;
  1465.         counter++;
  1466.     }
  1467.     m = ceil(sqrt(p)); /// may be change to sqrtl() ?
  1468.     tr1::unordered_map <int, int> mp;
  1469.     for (i = 0; i < m; i++){
  1470.         if (!mp[mul]) mp[mul] = i + 1;
  1471.         mul = (mul * g) % p;
  1472.     }
  1473.     for (i = 0; i < m; i++){
  1474.         z = extended_gcd(d, p, x, y);
  1475.         c = p / z;
  1476.         r = ((((long long)x * h) / z) % p + p) % p;
  1477.         if (mp[r]) return ((i * m) + mp[r] + counter - 1);
  1478.         d = (d * mul) % p;
  1479.     }
  1480.     return -1;
  1481. }
  1482. int main(){
  1483.     int g, h, p, res;
  1484.     for (; ;){
  1485.         scanf("%d %d %d", &g, &p, &h);
  1486.         if (!g && !p && !h) break;
  1487.         res = discrete_log(g, h % p, p);
  1488.         if (res == -1) puts("No Solution");
  1489.         else printf("%d\n", res);
  1490.     }
  1491.     return 0;
  1492. }
  1493.  
  1494.  
  1495. /// Discrete Log
  1496. int expo(long long x, int n, int m){
  1497.     long long res = 1;
  1498.  
  1499.     while (n){
  1500.         if (n & 1) res = (res * x) % m;
  1501.         x = (x * x) % m;
  1502.         n >>= 1;
  1503.     }
  1504.     return (res % m);
  1505. }
  1506. int extended_gcd(int a, int b, int& x, int& y){
  1507.     /// Bezout's identity, ax + by = gcd(a,b)
  1508.     if (!b){
  1509.         y = 0, x = 1;
  1510.         return a;
  1511.     }
  1512.     int g = extended_gcd(b, a % b, y, x);
  1513.     y -= ((a / b) * x);
  1514.     return g;
  1515. }
  1516. int inverse_modulo(int a, int m){
  1517.     /// inverse exists if and only if a and m are co-prime
  1518.     int x, y, inv;
  1519.     extended_gcd(a, m, x, y);
  1520.     inv = (x + m) % m;
  1521.     return inv;
  1522. }
  1523. int discrete_log(int g, int h, int p){
  1524.     /***
  1525.         * returns smallest x such that (g^x) % p = h, -1 if none exists
  1526.         * p must be a PRIME
  1527.         * function returns x, the discrete log of h with respect to g modulo p
  1528.     ***/
  1529.     if (h >= p) return -1;
  1530.     if ((g % p) == 0){
  1531.         if (h == 1) return 0; /// return -1 if strictly positive integer solution is required
  1532.         else return -1;
  1533.     }
  1534.     unordered_map <int, int> mp;
  1535.     int i, q, r, m = ceil(sqrt(p)); /// may be change to sqrtl() ?
  1536.     long long d = 1, inv = expo(inverse_modulo(g, p), m, p);
  1537.     for (i = 0; i <= m; i++){
  1538.         if (!mp[d]) mp[d] = i + 1;
  1539.         d *= g;
  1540.         if (d >= p) d %= p;
  1541.     }
  1542.     d = h;
  1543.     for (q = 0; q <= m; q++){
  1544.         r = mp[d];
  1545.         if (r) return ((m * q) + (--r));
  1546.         d *= inv;
  1547.         if (d >= p) d %= p;
  1548.     }
  1549.     return -1;
  1550. }
  1551. int main(){
  1552.     int T = 0, t, g, h, p;
  1553.     scanf("%d", &t);
  1554.     while (t--){
  1555.         scanf("%d %d %d", &g, &h, &p);
  1556.         int x = discrete_log(g, h, p);
  1557.         printf("Case %d: %d\n", ++T, x);
  1558.     }
  1559.     return 0;
  1560. }
  1561.  
  1562.  
  1563.  
  1564. /// Digits of Factorial
  1565. const double A = 4.0 * acos(0.0);
  1566. const double B = log10(exp(1.0));
  1567. long long digfact(long long n){
  1568.     if (n == 0 || n == 1) return 1;
  1569.     double x = ((0.5 * log(A * n)) + (n * log(n)) - n) * B;
  1570.     return ceil(x);
  1571. }
  1572. int main(){
  1573.     int t;
  1574.     long long n;
  1575.     scanf("%d", &t);
  1576.     while (t--){
  1577.         scanf("%lld", &n);
  1578.         printf("%lld\n", digfact(n));
  1579.     }
  1580.     return 0;
  1581. }
  1582.  
  1583.  
  1584.  
  1585. /// Chinese Remainder Theorem
  1586. #define dbg(x) cout << #x << " = " << x << endl
  1587. using namespace std;
  1588. long long extended_gcd(long long a, long long b, long long& x, long long& y){
  1589.     /// Bezout's identity, ax + by = gcd(a,b)
  1590.     if (!b){
  1591.         y = 0, x = 1;
  1592.         return a;
  1593.     }
  1594.     long long g = extended_gcd(b, a % b, y, x);
  1595.     y -= ((a / b) * x);
  1596.     return g;
  1597. }
  1598. long long mod_inverse(long long a, long long m){
  1599.     /// inverse exists if and only if a and m are co-prime
  1600.     long long x, y, inv;
  1601.     extended_gcd(a, m, x, y);
  1602.     inv = (x + m) % m;
  1603.     return inv;
  1604. }
  1605. long long CRT(int n, long long* ar, long long* mods){
  1606.     /// finds the unique solution x modulo M (product of mods) for which x % mods[i] = ar[i]
  1607.     /// mods must be pairwise co-prime
  1608.     int i, j;
  1609.     long long x, y, res = 0, M = 1;
  1610.     for (i = 0; i < n; i++) M *= mods[i];
  1611.     for (i = 0; i < n; i++){
  1612.         x = M / mods[i];
  1613.         y = mod_inverse(x, mods[i]);
  1614.         res = (res + (((x * ar[i]) % M) * y)) % M;
  1615.     }
  1616.     return res;
  1617. }
  1618. int main(){
  1619.     long long mods[] = {3, 5, 7};
  1620.     long long ar[] = {2, 3, 2};
  1621.     long long res = CRT(3, ar, mods);
  1622.     dbg(res);
  1623. }
  1624.  
  1625.  
  1626.  
  1627. /// Binomial Coefficients Modulo Prime Powers
  1628. #define MAXP 10000010
  1629. #define ran(a, b) ((((rand() << 15) ^ rand()) % ((b) - (a) + 1)) + (a))
  1630. using namespace std;
  1631. namespace crt{
  1632.     long long extended_gcd(long long a, long long b, long long& x, long long& y){
  1633.         if (!b){
  1634.             y = 0, x = 1;
  1635.             return a;
  1636.         }
  1637.  
  1638.         long long g = extended_gcd(b, a % b, y, x);
  1639.         y -= ((a / b) * x);
  1640.         return g;
  1641.     }
  1642.     long long mod_inverse(long long a, long long m){
  1643.         long long x, y, inv;
  1644.         extended_gcd(a, m, x, y);
  1645.         inv = (x + m) % m;
  1646.         return inv;
  1647.     }
  1648.     long long chinese_remainder(vector <long long> ar, vector <long long> mods){
  1649.         int i, j;
  1650.         long long x, y, res = 0, M = 1;
  1651.         for (i = 0; i < ar.size(); i++) M *= mods[i];
  1652.         for (i = 0; i < ar.size(); i++){
  1653.             x = M / mods[i];
  1654.             y = mod_inverse(x, mods[i]);
  1655.             res = (res + (((x * ar[i]) % M) * y)) % M;
  1656.         }
  1657.         return res;
  1658.     }
  1659. }
  1660. namespace bin{
  1661.     int dp[MAXP];
  1662.     long long mod = 0;
  1663.     long long trailing(long long x, long long p){
  1664.         long long res = 0;
  1665.         while (x){
  1666.             x /= p;
  1667.             res += x;
  1668.         }
  1669.         return res;
  1670.     }
  1671.     long long expo(long long x, long long n, long long m){
  1672.         if (!n) return 1;
  1673.         else if (n & 1) return ((expo(x, n - 1, m) * x) % m);
  1674.         else{
  1675.             long long r = expo(x, n >> 1, m);
  1676.             return ((r * r) % m);
  1677.         }
  1678.     }
  1679.     long long factorial(long long x, long long p){
  1680.         long long res = expo(dp[mod - 1], x / mod, mod);
  1681.         if (x >= p) res = res * factorial(x / p, p) % mod;
  1682.         return res * dp[x % mod] % mod;
  1683.     }
  1684.     /// binomial co-efficients modulo p^q (p must be a prime)
  1685.     long long binomial(long long n, long long k, long long p, long long q){
  1686.         if (k > n) return 0;
  1687.         if (n == k || k == 0) return 1;
  1688.         int i, j;
  1689.         for (i = 0, mod = 1; i < q; i++) mod *= p;
  1690.         long long t = trailing(n, p) - trailing(k, p) - trailing(n - k, p);
  1691.         if (t >= q) return 0;
  1692.         assert(mod < MAXP);
  1693.         for (dp[0] = 1, i = 1; i < mod; i++){
  1694.             dp[i] = (long long)dp[i - 1] * (i % p ? i : 1) % mod;
  1695.         }
  1696.         long long res = factorial(n, p) * expo(factorial(k, p) * factorial(n - k, p) % mod, (mod / p) * (p - 1) - 1, mod) % mod;
  1697.         while (t--) res = res * p % mod;
  1698.         return res;
  1699.     }
  1700.     /// binomial co-efficients modulo m (p_i^q_i of m must be less than MAXP)
  1701.     long long binomial(long long n, long long k, long long m){
  1702.         if (k > n || m == 1) return 0;
  1703.         if (n == k || k == 0) return 1;
  1704.         vector < pair <int, int > > factors;
  1705.         for (long long i = 2; i * i <= m; i++){
  1706.             int c = 0;
  1707.             while (m % i == 0){
  1708.                 c++;
  1709.                 m /= i;
  1710.             }
  1711.             if (c) factors.push_back(make_pair(i, c));
  1712.         }
  1713.         if (m > 1) factors.push_back(make_pair(m, 1));
  1714.         vector <long long> ar, mods;
  1715.         for (int i = 0; i < factors.size(); i++){
  1716.             long long x = 1;
  1717.             for (int j = 0; j < factors[i].second; j++) x *= factors[i].first;
  1718.             mods.push_back(x), ar.push_back(binomial(n, k, factors[i].first, factors[i].second));
  1719.         }
  1720.         return crt::chinese_remainder(ar, mods);
  1721.     }
  1722. }
  1723.  
  1724.  
  1725.  
  1726. ///All Divisors in Range
  1727. #define MAX 1000010
  1728. short len[MAX];
  1729. int L[MAX], *divisors[MAX];
  1730. void Generate(){
  1731.     int i, j, k, c, d, x;
  1732.     len[0] = len[1] = L[0] = L[1] = 1;
  1733.     for (i = 4; i < MAX; i++, i++) len[i] = 2;
  1734.     for (i = 3; (i * i) < MAX; i += 2){
  1735.         if (!len[i]){
  1736.             for (j = (i * i), d = i << 1; j < MAX; j += d){
  1737.                 if (!len[j]) len[j] = i;
  1738.             }
  1739.         }
  1740.     }
  1741.     for (i = 2; i < MAX; i++){
  1742.         if (!len[i]) L[i] = i;
  1743.         else{
  1744.             L[i] = len[i];
  1745.             x = L[i /len[i]];
  1746.             if (x > L[i]) L[i] = x;
  1747.         }
  1748.     }
  1749.     divisors[1] = (int *) malloc(sizeof(int));
  1750.     divisors[1][0] = 1, len[1] = 1;
  1751.     for (i = 2; i < MAX; i++){
  1752.         c = 0, k = i;
  1753.         while (k > 1 && L[k] == L[i]){
  1754.             c++;
  1755.             k /= L[k];
  1756.         }
  1757.         len[i] = (c + 1) * len[k];
  1758.         divisors[i] = (int *) malloc(sizeof(int) * len[i]);
  1759.         for (x = 1, j = 0, len[i] = 0; j <= c; j++, x *= L[i]){
  1760.             for (d = 0; d < len[k]; d++){
  1761.                 divisors[i][len[i]++] = divisors[k][d] * x;
  1762.             }
  1763.         }
  1764.     }
  1765. }
  1766. int main(){
  1767.     Generate();
  1768.     return 0;
  1769. }
  1770.  
  1771.  
  1772.  
  1773. /// Bernoulli Numbers
  1774. #define MAX 2510
  1775. #define MOD 1000000007
  1776. int S[MAX][MAX], inv[MAX], factorial[MAX], bernoulli[MAX];
  1777. int expo(long long x, int n){
  1778.     long long res = 1;
  1779.     while (n){
  1780.         if (n & 1) res = (res * x) % MOD;
  1781.         x = (x * x) % MOD;
  1782.         n >>= 1;
  1783.     }
  1784.     return (res % MOD);
  1785. }
  1786. void Generate(){
  1787.     int i, j;
  1788.     long long x, y, z, lim = (long long)MOD * MOD;
  1789.     for (i = 1, factorial[0] = 1; i < MAX; i++) factorial[i] = ((long long) factorial[i - 1] * i) % MOD;
  1790.     for (i = 0; i < MAX; i++) inv[i] = expo(i, MOD - 2);
  1791.     for (i = 1, S[0][0] = 1; i < MAX; i++){
  1792.         for (j = 1, S[i][0] = 0; j <= i; j++) S[i][j] = ( ((long long)S[i - 1][j] * j) + S[i - 1][j - 1]) % MOD;
  1793.     }
  1794.     bernoulli[0] = 1;
  1795.     for (i = 1; (i + 1) < MAX; i++){
  1796.         if ((i & 1) && i > 1) bernoulli[i] = 0;
  1797.         else{
  1798.             for (j = 0, x = 0, y = 0; j <= i; j++){
  1799.                 z = ((long long) factorial[j] * inv[j + 1]) % MOD;
  1800.                 z = (z * S[i][j]) % MOD;
  1801.                 if (j & 1) y += z;
  1802.                 else x += z;
  1803.             }
  1804.             bernoulli[i] = (lim + x - y) % MOD;
  1805.         }
  1806.     }
  1807. }
  1808. int main(){
  1809.     Generate();
  1810.     printf("%d\n", bernoulli[10]);
  1811.     return 0;
  1812. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement