DuongNhi99

PUZZLE

Mar 10th, 2022
566
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.06 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4. #define int long long
  5. const int MOD = 1e9 + 7;
  6. const int LIM = 1e6;
  7.  
  8. int fixMOD(ll v){
  9.     v %= MOD;
  10.     if (v < 0) v += MOD;
  11.     return v;
  12. }
  13.  
  14. int subMOD(int x, int y){
  15.     return fixMOD(x -= y);
  16. }
  17.  
  18. int mulMOD(ll x, ll y){
  19.     x %= MOD;
  20.     y %= MOD;
  21.     return fixMOD(x * y);
  22. }
  23.  
  24. int fact[LIM + 10];
  25. int tcaf[LIM + 10];
  26. int invs[LIM + 10];
  27.  
  28. void precal(int n = LIM){
  29.     fact[0] = fact[1] = 1;
  30.     tcaf[0] = tcaf[1] = 1;
  31.     invs[0] = invs[1] = 1;
  32.     for (int i = 2; i <= n; ++i){
  33.         fact[i] = mulMOD(fact[i - 1], i);
  34.         invs[i] = fixMOD(MOD - mulMOD(MOD / i, invs[MOD % i]));
  35.         tcaf[i] = mulMOD(tcaf[i - 1], invs[i]);
  36.     }
  37. }
  38.  
  39. int nck(int n, int k){
  40.     if (k < 0 || k > n) return 0;
  41.     return mulMOD(fact[n], mulMOD(tcaf[k], tcaf[n - k]));
  42. }
  43.  
  44. int m,n;
  45. long long x;
  46. long long path;
  47. int pcnt[LIM];
  48. int pos;
  49.  
  50. void factorize(vector<int> &S,long long x, int n){
  51.     for (int i = 1; i * i <= n; ++i) if (n % i == 0){
  52.         if (x % i == 0) S.push_back(i);
  53.         int j = n / i;
  54.         if (i != j && x % j == 0) S.push_back(j);
  55.     }
  56. }
  57.  
  58. long long pathSum(vector<int> S,int pos){
  59.     int k = S[pos];
  60.     long long cnt = nck(m/k+n/k,n/k);
  61.     if(k==1){
  62.         pcnt[1] = 1;
  63.         return cnt%MOD;
  64.     }
  65.     int mul = k;
  66.     pos--;
  67.     while(pos >=0){
  68.         if(k % S[pos] == 0){
  69.             mul -= pcnt[S[pos]];
  70.         }
  71.         pos--;
  72.     }
  73.     pcnt[k] = mul;
  74.     return (long long)(mul*cnt)%MOD;
  75. }
  76.  
  77. signed main(){
  78.     ios_base::sync_with_stdio(false);
  79.     cin.tie(NULL);
  80.     int t; cin >> t;
  81.     precal();
  82.     while(t--){
  83.         path =0;
  84.         cin >> x >> m >> n;
  85.         if(m == 1 && n ==1){
  86.             cout << mulMOD(x, x) << '\n';
  87.             continue;
  88.         }else if(n==1 || m == 1){
  89.             cout << x%MOD << '\n';
  90.             continue;
  91.         }
  92.         if(m > n) swap(m,n);
  93.         m--,n--;
  94.         vector<int> S;
  95.         factorize(S, x, n);
  96.         factorize(S, x, m);
  97.         sort(S.begin(),S.end());
  98.         S.resize(unique(S.begin(),S.end()) - S.begin());
  99.         int sz = S.size();
  100.         for(int i=0;i<sz;i++){
  101.             path = (path+pathSum(S,i)) %MOD;
  102.         }
  103.         cout << path << '\n';
  104.     }
  105. }
  106.  
Advertisement
Add Comment
Please, Sign In to add comment