标签:
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 9914 | Accepted: 3032 |
Description
Input
Output
Sample Input
2 4
Sample Output
12
用到素因子分解,高精度,以及集合中的互斥原理。代码如下:
1 #include <stdio.h> 2 3 long long factor[10000000]; 4 long long factor_num = 0; 5 6 long long M; 7 long long N; 8 9 10 void get_factor(long long num, long long factor[], long long *factor_num) 11 { 12 *factor_num = 0; 13 for (long long i = 2; i * i <= num ; i++) 14 { 15 if (num %i == 0) 16 { 17 *factor_num = (*factor_num) + 1; 18 factor[*factor_num] = i; 19 while( num % i == 0) 20 { 21 num = num / i; 22 } 23 } 24 } 25 if(num > 1) 26 { 27 *factor_num = (*factor_num) + 1; 28 factor[*factor_num] = num; 29 } 30 } 31 32 33 long long pow(long long x, long long index) 34 { 35 long long result = 1; 36 for (long long i = 1; i <= index; i++) 37 { 38 result *= x; 39 } 40 return result; 41 } 42 43 44 45 int main(int argc, char * argv[]) 46 { 47 48 scanf("%lld", &N); 49 scanf("%lld", &M); 50 51 //printf("N=%d M=%ld\n", N, M); 52 53 long long total = pow(M, N); 54 get_factor(M, factor, &factor_num); 55 56 //printf("total=%ld\n", total); 57 58 59 unsigned long long bi_factor_num = (1 << factor_num); 60 //printf("bi_factor_num=%d\n", bi_factor_num); 61 62 63 for (unsigned long long i = 1; i < bi_factor_num; i++) 64 { 65 //printf("%d-----\n", i); 66 long long t = 0; 67 long long facts = 1; 68 long long k = 1; 69 for (unsigned long long j = i; j >= 1; j = j >> 1) 70 { 71 if (j & 1) 72 { 73 t++; 74 facts *= factor[k]; 75 } 76 k++; 77 //printf("j =%d\n", j); 78 //printf("k =%d\n", k); 79 } 80 //printf("facts = %d %d %d\n", facts, t, pow(M/facts, N)); 81 if (t % 2 == 1) 82 { 83 total = total - pow(M/facts, N); 84 } 85 else if (t % 2 == 0) 86 { 87 total = total + pow(M/facts, N); 88 } 89 90 } 91 92 //printf("Done\n"); 93 printf("%lld\n", total); 94 return 0; 95 }
标签:
原文地址:http://www.cnblogs.com/ruichenduo/p/5951215.html