bzoj 4916 神犇和蒟蒻 杜教筛
第一题结合莫比乌斯函数定义,值恒为1。
第二题,phi(i^2) = phi(i) * i,根据欧拉函数的定义式能推出来,每个质因子的指数都增加一倍,都提出来一份,就是原先的phi(i)*i。然后还是跟g(x)卷一下,杜教筛即可。
1 #include <cstdio> 2 #include <map> 3 #include <cmath> 4 using namespace std; 5 typedef long long ll; 6 const int MAXN = 1000100,mo = 1e9 + 7,inv2 = (mo + 1) / 2,inv6 = (mo + 1) / 6; 7 8 int n,maxn,phi[MAXN],sum[MAXN],pri[MAXN]; 9 bool vis[MAXN]; 10 map<int,int> f; 11 int solve(int n) 12 { 13 if (n <= maxn) 14 return sum[n]; 15 if (f.count(n)) 16 return f[n]; 17 int ans = (ll)n * (n + 1) % mo * (n * 2 + 1) % mo * inv6 % mo; 18 for (int l = 2,r;l <= n;l = r + 1) 19 { 20 r = n / (n / l); 21 int tp = (ll)(l + r) * (r - l + 1) % mo * inv2 % mo; 22 ans -= (ll)tp * solve(n / l) % mo; 23 if (ans < 0) 24 ans += mo; 25 } 26 return f[n] = ans; 27 } 28 void init() 29 { 30 maxn = pow(n,2.0 / 3) + 0.5; 31 phi[1] = 1; 32 int tot = 0; 33 for (int i = 2;i <= maxn;i++) 34 { 35 if (vis[i] == false) 36 { 37 pri[++tot] = i; 38 phi[i] = i - 1; 39 } 40 for (int j = 1;j <= tot && i * pri[j] <= maxn;j++) 41 { 42 vis[i * pri[j]] = true; 43 if (i % pri[j] != 0) 44 phi[i * pri[j]] = phi[i] * (pri[j] - 1); 45 else 46 { 47 phi[i * pri[j]] = phi[i] * pri[j]; 48 break; 49 } 50 } 51 } 52 for (int i = 1;i <= maxn;i++) 53 sum[i] = (sum[i - 1] + (ll)phi[i] * i % mo) % mo; 54 } 55 56 int main() 57 { 58 scanf("%d",&n); 59 init(); 60 printf("1\n%d\n",solve(n)); 61 return 0; 62 }
心之所动 且就随缘去吧