D. Unusual Sequences(容斥)
阿新 • • 發佈:2018-01-20
post name urn () con blog opened contest std
D. Unusual Sequences
隔板法 + 容斥原理
1 //容斥(莫比烏斯反演) 2 #include <bits/stdc++.h> 3 using namespace std; 4 #define LL long long 5 const int mod = 1e9+7; 6 LL quickpow(LL a, LL b, LL mod){ 7 LL temp = a % mod, res = 1; 8 while(b){ 9 if(b & 1) res = res * temp % mod; 10 b >>= 1View Code; 11 temp = temp * temp % mod; 12 } 13 return res; 14 } 15 map<int, LL> mp; 16 17 LL solve(int x){ 18 if(x == 1) return 1; 19 if(mp.count(x)) return mp[x]; 20 mp[x] = quickpow(2, x-1, mod); 21 for(int i = 2; i * i <= x; i++){ 22 if(x % i == 0){ 23 mp[x] = (mp[x] - solve(i) + mod) % mod;24 if(i != x / i) mp[x] = (mp[x] - solve(x / i) + mod) % mod; 25 } 26 } 27 mp[x] = (mp[x] - solve(1) + mod) % mod; 28 return mp[x]; 29 } 30 31 int main(){ 32 ios::sync_with_stdio(0); 33 int x, y; 34 while(cin>>x>>y){ 35 if(y % x != 0){ 36 cout<<0<<endl; 37 }else{ 38 cout<<solve(y / x)<<endl; 39 40 } 41 } 42 43 }
D. Unusual Sequences(容斥)