LightOJ 1007 - Mathematically Hard (欧拉筛+预处理前缀和)
来源:互联网 发布:反监听软件 编辑:程序博客网 时间:2024/06/11 12:33
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;}
- LightOJ 1007 - Mathematically Hard (欧拉筛+预处理前缀和)
- LightOJ 1007 Mathematically Hard
- LightOJ 1007 Mathematically Hard
- lightoj 1007Mathematically Hard (欧拉函数水题)
- LightOJ1007-Mathematically Hard-maths,前缀和
- lightoj 1007 - Mathematically Hard 欧拉函数
- lightoj 1007 - Mathematically Hard 欧拉函数
- LightOJ 1007 Mathematically Hard 欧拉函数
- LightOJ-1007-Mathematically Hard [欧拉函数]
- Mathematically Hard LightOJ
- Light OJ 1007 Mathematically Hard [欧拉函数+前缀和]【数论】
- LightOJ1007 Mathematically Hard 欧拉函数+前缀和
- LIGHTOJ 1007 – MATHEMATICALLY HARD【欧拉函数】
- Light OJ 1007 - Mathematically Hard
- Light oj 1007 - Mathematically Hard
- light oj 1007 Mathematically Hard (欧拉函数)
- Mathematically Hard (欧拉定理)
- LightOJ1007 - Mathematically Hard
- 数据结构实验之排序四:寻找大富翁
- 图(最短路径)1
- 阿里云虚拟主机下载AKP失败解决办法
- zoj1025 dp(呃。。)
- 线程同步
- LightOJ 1007 - Mathematically Hard (欧拉筛+预处理前缀和)
- GDB多线程调试方法
- 我的成长(五)--- Make it Possible
- knockoutjs四 foreach的绑定
- 【转载】不再纠结:从程序员的角度解读苹果为什么坚持使用1G内存
- login与服务器交换数据
- Java调用webservice出现System.NullReferenceException: 未将对象引用设置到对象的实例。
- MySQL管理工具---phpAdmin
- 数模搭配上春运(My First Blog)