hdu6069 Counting Divisors 质因数分解 区间筛
来源:互联网 发布:如何投诉淘宝平台 编辑:程序博客网 时间:2024/05/30 12:31
http://acm.hdu.edu.cn/showproblem.php?pid=6069
Counting Divisors
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)Total Submission(s): 2757 Accepted Submission(s): 1020
Problem Description
In mathematics, the function d(n) denotes the number of divisors of positive integer n .
For example,d(12)=6 because 1,2,3,4,6,12 are all 12 's divisors.
In this problem, givenl,r and k , your task is to calculate the following thing :
(∑i=lrd(ik))mod998244353
For example,
In this problem, given
Input
The first line of the input contains an integer T(1≤T≤15) , denoting the number of test cases.
In each test case, there are3 integers l,r,k(1≤l≤r≤1012,r−l≤106,1≤k≤107) .
In each test case, there are
Output
For each test case, print a single line containing an integer, denoting the answer.
Sample Input
31 5 11 10 21 100 3
Sample Output
10482302
Source
2017 Multi-University Training Contest - Team 4
题意:d[i]:表示i的所有因子个数。现在求∑d(i^k),i∈[l,r]。
题解:首先每个数都可以分解成:i=p1^c1*p2^c2*...*pn^cn,其中p1,p2,...,pn均为质数。那么d[i]就可以表示成(p1+1)*(p2+1)*...*(pn+1),所以d[i^k]=(p1*k+1)*(p2*k+1)*...*(pn*k+1)。所以本题先对素数打表,但是直接枚举l-r仍然会超时。可以反过来枚举每个质数,用筛法的方式去枚举质数的每个倍数,这样就比原来快多了。
代码:
#include<bits/stdc++.h>#define debug cout<<"aaa"<<endl#define mem(a,b) memset(a,b,sizeof(a))#define LL long long#define lson l,mid,root<<1#define rson mid+1,r,root<<1|1#define MIN_INT (-2147483647-1)#define MAX_INT 2147483647#define MAX_LL 9223372036854775807i64#define MIN_LL (-9223372036854775807i64-1)using namespace std;const int N = 1000000 + 5;const LL mod = 998244353;int primes[N],tot=0,t;LL l,r,k;LL num[N],ans[N];bool isPrime[N];void getPrime()//素数筛 { mem(isPrime,true); for(int i=2;i<N;i++) { if(isPrime[i]) primes[++tot]=i; for(int j=1;j<=tot;j++) { if(i*primes[j]>=N) break; isPrime[i*primes[j]]=false; if(i%primes[j]==0) break; } }}int main(){getPrime();scanf("%d",&t);while(t--){scanf("%lld%lld%lld",&l,&r,&k);LL n=(r-l+1);for(int i=0;i<n;i++){//保存每个数字和其贡献值 num[i]=i+l;ans[i]=1;}for(int i=1;(LL)primes[i]*primes[i]<=r;i++){//枚举每个素数 for(LL j=primes[i]*(l/primes[i]);j<=r;j+=primes[i]){//枚举每个素数的倍数 if(j<l){continue;}LL cnt=0;while(num[j-l]%primes[i]==0){cnt++;num[j-l]/=primes[i];}ans[j-l]=(ans[j-l]*(1LL+cnt*k))%mod;}}LL sum=0;for(int i=0;i<n;i++){if(num[i]>1){//表示num[i]本身就是素数 ans[i]=(ans[i]*(1LL+k))%mod;}sum=(sum+ans[i])%mod;}printf("%lld\n",sum);}return 0;}
阅读全文
0 0
- hdu6069 Counting Divisors 质因数分解 区间筛
- HDU6069-区间筛&&因子公式-Counting Divisors
- HDU6069 Counting Divisors 区间素数筛法
- HDU6069 Counting Divisors(区间素数筛)
- hdu6069 Counting Divisors(质数分解+技巧)
- HDU6069 Counting Divisors(素数筛+二次筛)
- HDU6069 Counting Divisors
- hdu6069 Counting Divisors
- HDU6069 Counting Divisors【数学】
- HDU6069-Counting Divisors
- hdu6069 Counting Divisors
- Counting Divisors(hdu6069)
- hdu6069 Counting Divisors && lightoj1028
- HDU6069-Counting Divisors 约数个数定理+素数分解
- hdu6069 Counting Divisors(数学)
- 2017多校四 1003题 hdu 6069 Counting Divisors 分解质因数
- HDU6069多校第四场 Counting Divisors
- HDU6069-Counting Divisors(约数个数定理)
- dp专题3--树形dp入门
- Codeforces Round #420 (Div. 2) 821D Okabe and City 思维建图+spfa
- 模拟实现C++STL中的string类
- 【ZZULIOJ 2171 】 举世伐唐 【线段树 区间修改+区间最大值】
- python实现两个任意字符串乘积
- hdu6069 Counting Divisors 质因数分解 区间筛
- 【Mybatis】sqlMapConfig.xml配置文件解析
- MySQL存储过程写法总结
- JavaScript作用域链和原型链
- 2018届秋招提前批阿里在线编程
- HTML5 总结
- Web 项目中 JSP 和 HTML 的取舍和资源引用时 "\" 根目录的寻找
- 状压DP 拯救莫莉斯
- Good Luck in CET-4 Everybody!(博弈)