LightOJ 1007 - Mathematically Hard (欧拉筛+预处理前缀和)
2016-02-05 21:18
826 查看
1007 - Mathematically Hard
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 from
ato b (inclusive). The score of a number is defined as thefollowing function.
score (x) = n2, where n is thenumber of relatively prime numbers with
x, 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.
Each case will contain two integers a and b (2≤ a ≤ b ≤ 5 * 106).
a to b.
![](https://oscdn.geek-share.com/Uploads/Images/Content/202005/09/ddd2552c7e4812e6e91afc8b356e155d.gif)
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.
![](https://oscdn.geek-share.com/Uploads/Images/Content/202005/09/ddd2552c7e4812e6e91afc8b356e155d.gif)
is read "phi of n."
Given the general prime factorization of
![](https://oscdn.geek-share.com/Uploads/Images/Content/202005/09/f692597b66fb5b9baef4f9aaeaea9efd.gif)
, one cancompute
![](https://oscdn.geek-share.com/Uploads/Images/Content/202005/09/ddd2552c7e4812e6e91afc8b356e155d.gif)
using
the formula
![](https://oscdn.geek-share.com/Uploads/Images/Content/202005/09/53d540ba7e4ac7a2677696f8e6e7b689.gif)
题意:给你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-10
using 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;}
//head
LL 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;
}
![]() ![]() | PDF (English) | Statistics | Forum |
Time Limit: 2 second(s) | Memory Limit: 64 MB |
In this problem, you will be given two integers a andb. You have to find the summation of the scores of the numbers from
ato b (inclusive). The score of a number is defined as thefollowing function.
score (x) = n2, where n is thenumber of relatively prime numbers with
x, 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 froma 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![](https://oscdn.geek-share.com/Uploads/Images/Content/202005/09/ddd2552c7e4812e6e91afc8b356e155d.gif)
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.
![](https://oscdn.geek-share.com/Uploads/Images/Content/202005/09/ddd2552c7e4812e6e91afc8b356e155d.gif)
is read "phi of n."
Given the general prime factorization of
![](https://oscdn.geek-share.com/Uploads/Images/Content/202005/09/f692597b66fb5b9baef4f9aaeaea9efd.gif)
, one cancompute
![](https://oscdn.geek-share.com/Uploads/Images/Content/202005/09/ddd2552c7e4812e6e91afc8b356e155d.gif)
using
the formula
![](https://oscdn.geek-share.com/Uploads/Images/Content/202005/09/53d540ba7e4ac7a2677696f8e6e7b689.gif)
题意:给你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-10
using 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;}
//head
LL 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;
}
相关文章推荐
- android 加载大图片适配问题解决方案
- login与服务器交换数据
- Java调用webservice出现System.NullReferenceException: 未将对象引用设置到对象的实例。
- Mybatis缓存
- 【转载】不再纠结:从程序员的角度解读苹果为什么坚持使用1G内存
- knockoutjs四 foreach的绑定
- 获取WebView里的网页文本内容
- java学习之坦克大战游戏
- 实现支付宝咻一咻的几种思路
- JavaWeb将数据包装成JSON以及JSP通过ajax对JSON的获取与解析(代码实例)
- 阿里云虚拟主机下载AKP失败解决办法
- zoj1025 dp(呃。。)
- 【Java】java使用反射访问对象方法和成员变量
- poj 1696 Space Ant(模拟+叉积)
- 数据结构实验之排序四:寻找大富翁
- nyoj 1237 最大岛屿(dfs)
- 单片机片内存储器烧写
- 第7讲项目3——发工资
- 使用DataGridView进行增删改查 并同步到数据库
- C语言 gets()和scanf()函数的区别