LightOJ 1007 Mathematically Hard(欧拉函数)

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) (2ab5106).

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 ab的所有欧拉函数值之和

题解
筛出 1 − 5 e 6 1-5e6 15e6的所有的欧拉函数值,跑一遍前缀和,需要注意的是,会爆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;
}


/*


*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值