2015东北四省赛 L题 线性筛+积性函数 求因子和
2015-06-17 20:46
344 查看
Given an interval [x, y], calculate the sum of all the factors of the whole integers from x to y (include x and y).
Let describe it officially. Let's definite f (i) is the sum of factors of i, you should work out S(x, y), here S(x, y) is:
f (2) = 1 + 2 = 3
f (3) = 1 + 3 = 4
f (4) = 1 + 2 + 4 = 7
f (5) = 1 + 5 = 6
f (6) = 1 + 2 + 3 + 6 = 12
So the answer is 1 + 3 + 4 + 7 + 6 + 12 = 33
Let describe it officially. Let's definite f (i) is the sum of factors of i, you should work out S(x, y), here S(x, y) is:
输入
There are multiple test cases, the number of cases T (1 ≤ T ≤ 105) will be given in the first line of input data. Each test case will include two integer x and y (1 ≤ x < y ≤ 5 × 106).输出
For the ith test case, output “Case $i:” in the first line. And print the answer of S(x, y) in a single line. The answer may be very large, so moudule 10000007 before printing it.样例输入
31 65 1212 300
样例输出
Case $1:33Case $2:112Case $3:74210
提示
F (1) = 1 = 1f (2) = 1 + 2 = 3
f (3) = 1 + 3 = 4
f (4) = 1 + 2 + 4 = 7
f (5) = 1 + 5 = 6
f (6) = 1 + 2 + 3 + 6 = 12
So the answer is 1 + 3 + 4 + 7 + 6 + 12 = 33
来源
2015大连海事#include <iostream> #include<stdio.h> #include<string.h> #include<algorithm> #define ll long long using namespace std; const int maxn=5000000+10; const int mod=10000007; int vis[maxn]; ll prime[maxn]; ll fac[maxn];//因子和积性函数 int num[maxn];//i这个数的最小质因子的幂指数 int qpow(int a,int b) { int res=1; while(b) { if(b&1) res=res*a; a=a*a; b=b/2; } return res; } void init() { memset(vis,0,sizeof(vis)); memset(num,0,sizeof(num)); int cnt=0; fac[1]=1; fac[0]=0; for(ll i=2;i<=maxn;i++) { if(!vis[i]) { prime[cnt++]=i; fac[i]=1+i; num[i]=1; } for(ll j=0;j<cnt&&i*prime[j]<=maxn;j++) { vis[i*prime[j]]=1;//该数的最小质因子为prime[j] if(i%prime[j]==0)//若这个数为2*6 或者为2*4(2^3) { num[i*prime[j]]=num[i]+1; //判断这个数是否为p^k次方形式 int res=i; while(res%prime[j]==0) {res=res/prime[j];} int ok=1; if(res==1) ok=0; // if(ok==0)//若为p^k次方形式,则需单独计算 f[p^k]=f[p^k-1]+p^k; fac[i*prime[j]]=fac[i]+qpow(prime[j],num[i]+1); else//若不为,则f =f[p^k]*f[n/(p^k)]; fac[i*prime[j]]=fac[i/qpow(prime[j],num[i])]*fac[qpow(prime[j],num[i]+1)]; break; } else//若这个数为互质的数相乘 { num[i*prime[j]]=1; fac[i*prime[j]]=fac[i]*fac[prime[j]]; } } } // for(int i=1;i<=12;i++) // printf("%d ",fac[i]); for(int i=2;i<=maxn;i++) fac[i]=(fac[i-1]+fac[i])%mod; } int main() { init(); int t; int cas=1; cin>>t; while(t--) { int x,y; scanf("%d%d",&x,&y); printf("Case $%d:\n",cas++); printf("%lld\n",(fac[y]-fac[x-1]+mod)%mod); } }
相关文章推荐
- 1252进制转换
- 二叉树的常见问题及其解决思路
- Problem A: Artificial Intelligence?
- linux sed命令详解
- Java反射机制浅析
- Sqlite数据库锁死问题
- shell tr 命令
- shell 的cut 命令用法
- 贝叶斯公式和朴素贝叶斯分类算法
- HIVE动态分区实战
- Best Time to Buy and Sell Stock II
- 技术选型
- [Swust OJ 610]--吉祥数
- oj刷题第一批E 对象数组求最大值
- 【hadoop】ssh localhost 免密码登陆(图解)
- shell中if语句特殊参数
- 第十四周项目2-用文件保存的学生名单
- VC中使用纯资源dll实现多语言版本
- hive中sql解析出对应表和字段的调查
- WorldWind源码剖析系列:可渲染对象类RenderableObject