Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.93 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define TESTC ""
  6. #define PROBLEM "432"
  7.  
  8. #define USE_CPPIO() ios_base::sync_with_stdio(0); cin.tie(0)
  9.  
  10. #define maxn 1000005
  11.  
  12. typedef long long LL;
  13. LL n,m;
  14. LL x,y;
  15. LL gia[maxn];
  16. LL gia_prime[maxn][32];
  17. vector <int> prime;
  18.  
  19.  
  20. LL pw(LL x,LL y){
  21.  
  22.     LL ret=1,tmp=x%m;
  23.     while(y){
  24.         if(y&1)ret=ret*tmp%m;
  25.         tmp=tmp*tmp%m;
  26.         y>>=1;
  27.     }
  28.     return ret;
  29. }
  30.  
  31. void build()//建階層表(mod版)
  32. {
  33.     prime.clear();
  34.     LL tmp_m = m;
  35.  
  36.     for (int i = 2; i <= (LL)sqrt(tmp_m); ++i)
  37.     {
  38.         if (tmp_m % i)
  39.         {
  40.             continue;
  41.         }
  42.         prime.push_back(i);
  43.         while(tmp_m % i == 0)
  44.         {
  45.             tmp_m /= i;
  46.         }
  47.     }
  48.  
  49.     if (tmp_m > 1)
  50.     {
  51.         prime.push_back((int)tmp_m);
  52.     }
  53.     gia[0] = 1;
  54.  
  55.     memset(gia_prime,0,sizeof(gia_prime));
  56.  
  57.     for (int i = 1; i < maxn; ++i)
  58.     {
  59.         LL tmp = i;
  60.         for (int j = 0; j < prime.size(); ++j)
  61.         {
  62.             gia_prime[i][j] = gia_prime[i-1][j];
  63.             while(tmp % prime[j] == 0)
  64.             {
  65.                 tmp /= prime[j];
  66.                 gia_prime[i][j]++;
  67.             }
  68.         }
  69.         gia[i] = gia[i-1] * tmp % m;
  70.     }
  71.  
  72. }
  73.  
  74. void gcdex(LL a,LL b,LL c,LL *x,LL *y)//gcdex求模逆元
  75. {
  76.     if (b == 0)
  77.     {
  78.         *x = c/a;
  79.         *y = 0;
  80.         return;
  81.     }
  82.  
  83.     gcdex(b,a%b,c,x,y);
  84.     LL old_x = *x;
  85.     *x = *y;
  86.     *y = old_x - a/b * (*y);
  87.     return;
  88. }
  89.  
  90.  
  91. int main()
  92. {
  93.     #ifdef DBG
  94.     freopen(PROBLEM TESTC ".in", "r", stdin);
  95.     freopen(PROBLEM ".out", "w", stdout);
  96.     #endif
  97.  
  98.     scanf("%lld %lld",&n,&m);
  99.  
  100.     build();
  101.  
  102.     while(n--)
  103.     {
  104.  
  105.         scanf("%lld %lld",&x,&y);
  106.         LL ans = 1;
  107.  
  108.         for (int i = 0; i < prime.size(); ++i)
  109.         {
  110.             ans = ans * pw(prime[i],gia_prime[x][i] - gia_prime[x-y][i] - gia_prime[y][i]) % m;
  111.         }
  112.  
  113.         ans = ans * gia[x] % m;
  114.  
  115.         LL xx = 0,yy = 0;
  116.         gcdex(gia[y],m,(LL)1,&xx,&yy);
  117.         LL tmp3 = (xx + m) % m;
  118.         ans = ans * tmp3 % m;
  119.  
  120.         xx = 0,yy = 0;
  121.         gcdex(gia[x-y],m,(LL)1,&xx,&yy);
  122.         LL tmp2 = (xx + m) % m;
  123.         ans = ans * tmp2 % m;
  124.  
  125.  
  126.         printf("%lld\n",ans);
  127.     }
  128.  
  129.  
  130.  
  131.  
  132.     return 0;
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement