欧拉函数平方和
备注:1、线性筛的线性机构巧用
2、unsigned long long ans[maxn+6]
定义函数 varphi(x)φ(x) 为 11 到 x-1x−1 与 xx 互质的个数。
现在给定一段区间 [l,r][l,r],要求计算下列式子:
sum_{i=l}^rvarphi(i)^2∑i=lrφ(i)2
输入
输入以整数 T(1≤T≤10^5)T(1≤T≤105)开始,表示测试用例的数量。
每个测试用例将包含整数 l,r (2 ≤ l ≤ r ≤ 5 * 10^{6})l,r(2≤l≤r≤5∗106)。
输出
共有 TT 行。对于每组测试数据,输出一行信息 "Case t: A" (不含引号)。
其中 tt 表示对应的是第几组测试数据,A 表示对应的答案。
样例
输入
3 6 6 8 8 2 20
输出
Case 1: 4 Case 2: 16 Case 3: 1237
1 /************************************************************************* 2 > File Name: 4810.cpp 3 > Author: Henry Chen 4 > Mail: 390989083@qq.com 5 > Created Time: 日 9/ 6 22:03:35 2020 6 ************************************************************************/ 7 8 #include<bits/stdc++.h> 9 using namespace std; 10 const int maxn = 5000000; 11 unsigned long long ans[maxn+6]; 12 int prime[maxn+6]; 13 int tot = 0; 14 void find_prime() 15 { 16 for(int i = 2;i <= maxn;i++) 17 { 18 if(!ans[i]) 19 { 20 prime[++tot] = i; 21 ans[i] = i-1; 22 } 23 for(int j = 1;j <= tot&&i*prime[j] <= maxn;j++) 24 { 25 if(i % prime[j] == 0) 26 { 27 ans[i*prime[j]] = ans[i]*prime[j]; 28 break; 29 } 30 ans[i*prime[j]] = ans[i]*(prime[j]-1); 31 } 32 } 33 } 34 int main() 35 { 36 ans[1] = 1; 37 find_prime(); 38 //puts("debug"); 39 for(int i = 1;i <= maxn;i++) 40 { 41 ans[i] = ans[i-1]+1ll*ans[i]*ans[i]; 42 } 43 //for(int i = 1;i <= 20;i++) 44 //{ 45 // printf("%d ",ans[i]); 46 //} 47 int t; 48 //puts("emm"); 49 cin >> t; 50 for(int i = 1;i <= t;i++) 51 { 52 int l,r; 53 scanf("%d%d",&l,&r); 54 printf("Case %d: %llu ",i,ans[r]-ans[l-1]); 55 } 56 return 0; 57 }