LightOJ 1007 - Mathematically Hard (欧拉筛+预处理前缀和)

来源:互联网 发布:反监听软件 编辑:程序博客网 时间:2024/06/11 12:33
1007 - Mathematically Hard
  PDF (English)StatisticsForum
Time Limit: 2 second(s)Memory Limit: 64 MB

Mathematically some problems look hard. But with the help ofthe computer, some problems can be easily solvable.

In this problem, you will be given two integers a andb. You have to find the summation of the scores of the numbers fromato b (inclusive). The score of a number is defined as thefollowing function.

score (x) = n2, where n is thenumber of relatively prime numbers withx, which are smaller than x

For example,

For 6, the relatively prime numbers with 6 are 1 and 5. So,score (6) = 22 = 4.

For 8, the relatively prime numbers with 8 are 1, 3, 5 and7. So, score (8) = 42 = 16.

Now you have to solve this task.

Input

Input starts with an integer T (≤ 105),denoting the number of test cases.

Each case will contain two integers a and b (2≤ a ≤ b ≤ 5 * 106).

Output

For each case, print the case number and the summation of allthe scores from a to b.

Sample Input

Output for Sample Input

3

6 6

8 8

2 20

Case 1: 4

Case 2: 16

Case 3: 1237

Note

Euler's totient function  applied to a positive integer n is defined to be the numberof positive integers less than or equal to n that are relatively prime to n is read "phi of n."

Given the general prime factorization of , one cancompute  using the formula



题意:给你L和R,求这个范围内的每个数的欧拉函数的平方和


思路:看一下数据范围5e6,不大,打表就行,先筛一下欧拉数,然后预处理前缀和,因为最后结果会爆long long,所以用unsign long long


ac代码:

#include<stdio.h>#include<math.h>#include<string.h>#include<stack>#include<set>#include<queue>#include<vector>#include<iostream>#include<algorithm>#define MAXN 5000010#define LL unsigned long long#define ll __int64#define INF 0x7fffffff#define mem(x) memset(x,0,sizeof(x))#define PI acos(-1)#define eps 1e-10using namespace std;int gcd(int a,int b){return b?gcd(b,a%b):a;}int lcm(int a,int b){return a/gcd(a,b)*b;}LL powmod(LL a,LL b,LL MOD){LL ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}//headLL ans[MAXN];void db(){mem(ans);ans[1]=1;int i,j;for(i=2;i<=MAXN;i++){if(!ans[i]){for(j=i;j<=MAXN;j+=i){if(!ans[j])ans[j]=j;ans[j]=ans[j]/i*(i-1);    }}}for(i=1;i<=MAXN;i++)ans[i]=ans[i-1]+ans[i]*ans[i];}int main(){db();int t,l,r;int cas=0;scanf("%d",&t);while(t--){scanf("%d%d",&l,&r);printf("Case %d: ",++cas);printf("%llu\n",ans[r]-ans[l-1]);}return 0;}


0 0
原创粉丝点击