Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 8482 | Accepted: 2514 |
Description
Input
Output
Sample Input
2 4
Sample Output
12
Hint
Source
1 import java.math.BigInteger; 2 import java.util.Scanner; 3 4 5 public class Main { 6 7 static int yz[] = new int[1002]; 8 static int Q[] = new int[2002]; 9 static int len = 0; 10 static int qlen = 0; 11 public static void main(String[] args) { 12 Scanner cin = new Scanner(System.in); 13 while(cin.hasNext()){ 14 int n = cin.nextInt(); 15 int m = cin.nextInt(); 16 BigInteger sum = BigInteger.valueOf(m); 17 sum = sum.pow(n); 18 /* 19 * 求m的素因子,并容斥 20 */ 21 init(m); 22 BigInteger tmp = BigInteger.ZERO; 23 BigInteger sum2 = BigInteger.ZERO; 24 for(int i=1;i<=qlen;i++) 25 { 26 if(Q[i]>0) 27 { 28 int k = m/Q[i]; 29 tmp=BigInteger.valueOf(k); 30 tmp = tmp.pow(n); 31 sum2=sum2.add(tmp); 32 } 33 else if(Q[i]<0) 34 { 35 Q[i] = -Q[i]; 36 int k = m/Q[i]; 37 tmp = BigInteger.valueOf(k); 38 tmp = tmp.pow(n); 39 tmp = tmp.multiply(BigInteger.valueOf(-1)); 40 sum2=sum2.add(tmp); 41 } 42 } 43 sum2=sum2.multiply(BigInteger.valueOf(-1)); 44 sum=sum.add(sum2); 45 System.out.println(sum); 46 } 47 } 48 49 private static void init(int n) { 50 len = 0; 51 for(int i=2;i<=n/i;i++) 52 { 53 if(n%i==0) 54 { 55 while(n%i==0) 56 n=n/i; 57 yz[++len] = i; 58 } 59 } 60 if(n!=1) yz[++len] = n; 61 qlen = 0; 62 Q[0]=-1; 63 for(int i=1;i<=len;i++) 64 { 65 int k = qlen; 66 for(int j=0;j<=k;j++) 67 Q[++qlen]=-1*Q[j]*yz[i]; 68 } 69 } 70 }