HDU5942 Just a Math Problem
这个题关键问题是转化2的f(i) 的值变为将i的质因子分组的问题。
以后遇到2的幂次需要往这方面考虑。
1 #include <bits/stdc++.h> 2 const long long mod = 1e9+7; 3 const double ex = 1e-10; 4 #define inf 0x3f3f3f3f 5 using namespace std; 6 long long not_prim[1000050],prim[1000050]; 7 vector <int> x[1000011]; 8 long long mu[1000050],tot; 9 void getmu(long long n){ 10 not_prim[0] = not_prim[1] = 1; 11 mu[1] = 1; 12 for (long long i = 2 ; i<= n; i++){ 13 if (!not_prim[i]){ 14 mu[i] = -1; 15 prim[++tot] = i; 16 } 17 for (long long j = 1; j <= tot && prim[j]*i <= n ; j++){ 18 not_prim[prim[j]*i] = i; 19 if (i % prim[j] == 0){ 20 mu[prim[j]*i] = 0; 21 break; 22 } 23 mu[prim[j]*i] =-mu[i]; 24 } 25 } 26 for (int i = 1 ; i <= n ;i++){ 27 for (int j = i ; j <= n; j+=i) 28 x[j].push_back(i); 29 } 30 } 31 long long dp[1000011]; 32 inline long long solve(long long n, long long m){ 33 if (m==n && dp[n]!=0) return dp[n]; 34 long long ans = 0; 35 int num = x[m].size(); 36 for (int i = 0; i<num ;i++){ 37 ans =( ans + ( mu[x[m][i]]*(n/x[m][i]) % mod) + mod ) % mod; 38 } 39 if (m==n) dp[n] = ans; 40 return ans; 41 } 42 int main() 43 { 44 int T; 45 getmu(1000000); 46 cin >> T; 47 int cas = 1; 48 while (T--){ 49 long long N; 50 cin >> N; 51 long long ans = 0; 52 for (long long i = 1 ; i*i <= N ;i++){ 53 ans = (ans + solve(N/i,i) - solve(i,i) + mod ) % mod; 54 } 55 ans = (ans * 2 + 1 ) % mod; 56 printf("Case #%d: %lld\n",cas++,ans); 57 } 58 return 0; 59 }