Mathematically Hard
Mathematically some problems look hard. But with the help of the computer, some problems can be easily solvable.
In this problem, you will be given two integers a and b. You have to find the summation of the scores of the numbers from a to b (inclusive). The score of a number is defined as the following function.
score ( x ) = n 2 (x) = n^2 (x)=n2, where n is the number of relatively prime numbers with x x x, which are smaller than x x x
For example,
For 6, the relatively prime numbers with 6 are 1 and 5. So, s c o r e ( 6 ) = 22 = 4 score (6) = 22 = 4 score(6)=22=4.
For 8, the relatively prime numbers with 8 are 1, 3, 5 and 7. So, s c o r e ( 8 ) = 42 = 16 score (8) = 42 = 16 score(8)=42=16.
Now you have to solve this task.
Input
Input starts with an integer
T
(
≤
1
0
5
)
T (≤ 10^5)
T(≤105), denoting the number of test cases.
Each case will contain two integers a and b ( 2 ≤ a ≤ b ≤ 5 ∗ 1 0 6 ) (2 ≤ a ≤ b ≤ 5 * 10^6) (2≤a≤b≤5∗106).
Output
For each case, print the case number and the summation of all the scores from a to b.
Sample Input
3
6 6
8 8
2 20
Sample Output
Case 1: 4
Case 2: 16
Case 3: 1237
题意
求
a
到
b
a到b
a到b的所有欧拉函数值之和
题解
筛出
1
−
5
e
6
1-5e6
1−5e6的所有的欧拉函数值,跑一遍前缀和,需要注意的是,会爆long long。所以要开ull,另外求和如果单独开一个数组会超内存
代码
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <queue>
#include <cmath>
#include <string>
#include <cstring>
#include <map>
#include <set>
#include <cmath>
using namespace std;
#define me(x,y) memset(x,y,sizeof x)
#define MIN(x,y) x < y ? x : y
#define MAX(x,y) x > y ? x : y
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 5e6;
const ll INF = 0x3f3f3f3f;
const int MOD = 998244353;
const double eps = 1e-06;
vector<int>prime;
ull phi[maxn+10];
bool vis[maxn+10];
int tot = 0;
void get_phi(){
me(vis,false);
for(int i = 2; i <= maxn; ++i){
if(!vis[i]) prime.push_back(i),phi[i] = i-1;
for(int j = 0; j < prime.size() && i*prime[j] <= maxn; ++j){
vis[i*prime[j]] = true;
if(i%prime[j] == 0){
phi[i*prime[j]] = phi[i]*(ull)prime[j];
break;
}
phi[i*prime[j]] = phi[i]*phi[prime[j]];
}
}
phi[0] = 0;
for(int i = 1; i <= maxn; ++i){
ull tmp = phi[i]*phi[i];
phi[i] = phi[i-1]+tmp;
}
}
int main(){
int t;
cin>>t;
get_phi();
for(int ca = 1; ca <= t; ca++){
int a,b;
scanf("%d%d",&a,&b);
printf("Case %d: %llu\n",ca,phi[b]-phi[a-1]);
}
return 0;
}
/*
*/