Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 8910 | Accepted: 2676 |
Description
Input
Output
Sample Input
2 4
Sample Output
12
Hint
Source
import java.util.*; import java.math.*; public class Main{ public static int fac[] = new int[105]; public static int re[] = new int[105]; public static int cnt = 0, n, m; public static BigInteger fnum; public static void Divide(int x){ //分解质因数 for(int i = 2; i <= (int)Math.sqrt(x); i++){ if(x % i == 0){ fac[cnt ++] = i; while(x % i == 0){ x = x / i; } } } if(x > 1) fac[cnt ++] = x; } public static void DFS(int pos, int cur, int num){ if(cur == num){ BigInteger t = BigInteger.valueOf(m); for(int i = 0; i < cur; i++) //t表示gcd值为num个素数相乘得到时(设乘积为mul)每个元素可能的值即(m / mul) //比如m=18,mul=6时,每个元素可能的值有3个:6 12 18 t = t.divide(BigInteger.valueOf(re[i])); //每个位置有t种选择,共有n各位置,所以当前集合元素个数为t^n fnum = fnum.add(t.pow(n)); return; } for(int i = pos; i < cnt; i++){ re[cur] = fac[i]; DFS(i + 1, cur + 1, num); } } public static void main(String[] args){ Scanner in = new Scanner(System.in); while(in.hasNext()) { n = in.nextInt(); m = in.nextInt(); Divide(m); BigInteger extra = BigInteger.ZERO; for(int i = 1; i <= cnt; i++){ fnum = BigInteger.ZERO; //fnum存的是gcd集合元素个数 DFS(0, 0, i); if(i % 2 == 1) //容斥 extra = extra.add(fnum); else extra = extra.subtract(fnum); } System.out.println(BigInteger.valueOf(m).pow(n).subtract(extra)); } } }