标签:
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 9225 | Accepted: 2762 |
Description
Input
Output
Sample Input
2 4
Sample Output
12
Hint
Source
1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 #include <stack> 5 #include <queue> 6 #include <map> 7 #include <algorithm> 8 #include <vector> 9 #include <cmath> 10 11 using namespace std; 12 13 const int maxn = 1000005; 14 15 typedef long long LL; 16 17 int fac[1005]; 18 int t; 19 20 void divide(int m) 21 { 22 int i; 23 t = 0; 24 int k = (int)sqrt(m); 25 for(int i=2;i<=k;i++){ 26 if(m%i == 0){ 27 t++; 28 fac[t] = i; 29 while(m%i == 0) 30 m /= i; 31 } 32 } 33 if(m != 1){ 34 t++; 35 fac[t] = m; 36 } 37 } 38 LL quick_multi(LL a,LL b) 39 { 40 LL ans = 1; 41 while(b){ 42 ans *= a; 43 b--; 44 } 45 return ans; 46 } 47 48 LL ans,temp; 49 int a[100],m,n; 50 51 void dfs(int b,int ct,int c) //有c个公因子 52 { 53 int i,x; 54 if(ct == c){ 55 x = m; 56 for(i=1;i<=c;i++){ 57 x /= a[i]; 58 } 59 temp += quick_multi(x,n); 60 return ; 61 } 62 for(i = b+1;i<=t;i++){ 63 a[ct+1] = fac[i]; 64 dfs(i,ct+1,c); 65 } 66 } 67 int main() 68 { 69 int i; 70 while(scanf("%d%d",&n,&m)!=EOF){ 71 ans = t = 0; 72 divide(m); 73 for(int i=1;i<=t;i++){ 74 temp = 0; 75 dfs(0,0,i); 76 if(i&1){ 77 ans += temp; 78 } 79 else 80 ans -= temp; 81 } 82 ans = quick_multi(m,n) - ans; 83 printf("%lld\n",ans); 84 } 85 86 return 0; 87 }
标签:
原文地址:http://www.cnblogs.com/lmlyzxiao/p/4937637.html