Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define TESTC ""
- #define PROBLEM "432"
- #define USE_CPPIO() ios_base::sync_with_stdio(0); cin.tie(0)
- #define maxn 1000005
- typedef long long LL;
- LL n,m;
- LL x,y;
- LL gia[maxn];
- LL gia_prime[maxn][32];
- vector <int> prime;
- LL pw(LL x,LL y){
- LL ret=1,tmp=x%m;
- while(y){
- if(y&1)ret=ret*tmp%m;
- tmp=tmp*tmp%m;
- y>>=1;
- }
- return ret;
- }
- void build()//建階層表(mod版)
- {
- prime.clear();
- LL tmp_m = m;
- for (int i = 2; i <= (LL)sqrt(tmp_m); ++i)
- {
- if (tmp_m % i)
- {
- continue;
- }
- prime.push_back(i);
- while(tmp_m % i == 0)
- {
- tmp_m /= i;
- }
- }
- if (tmp_m > 1)
- {
- prime.push_back((int)tmp_m);
- }
- gia[0] = 1;
- memset(gia_prime,0,sizeof(gia_prime));
- for (int i = 1; i < maxn; ++i)
- {
- LL tmp = i;
- for (int j = 0; j < prime.size(); ++j)
- {
- gia_prime[i][j] = gia_prime[i-1][j];
- while(tmp % prime[j] == 0)
- {
- tmp /= prime[j];
- gia_prime[i][j]++;
- }
- }
- gia[i] = gia[i-1] * tmp % m;
- }
- }
- void gcdex(LL a,LL b,LL c,LL *x,LL *y)//gcdex求模逆元
- {
- if (b == 0)
- {
- *x = c/a;
- *y = 0;
- return;
- }
- gcdex(b,a%b,c,x,y);
- LL old_x = *x;
- *x = *y;
- *y = old_x - a/b * (*y);
- return;
- }
- int main()
- {
- #ifdef DBG
- freopen(PROBLEM TESTC ".in", "r", stdin);
- freopen(PROBLEM ".out", "w", stdout);
- #endif
- scanf("%lld %lld",&n,&m);
- build();
- while(n--)
- {
- scanf("%lld %lld",&x,&y);
- LL ans = 1;
- for (int i = 0; i < prime.size(); ++i)
- {
- ans = ans * pw(prime[i],gia_prime[x][i] - gia_prime[x-y][i] - gia_prime[y][i]) % m;
- }
- ans = ans * gia[x] % m;
- LL xx = 0,yy = 0;
- gcdex(gia[y],m,(LL)1,&xx,&yy);
- LL tmp3 = (xx + m) % m;
- ans = ans * tmp3 % m;
- xx = 0,yy = 0;
- gcdex(gia[x-y],m,(LL)1,&xx,&yy);
- LL tmp2 = (xx + m) % m;
- ans = ans * tmp2 % m;
- printf("%lld\n",ans);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement