首页 技术 正文
技术 2022年11月21日
0 收藏 631 点赞 4,813 浏览 1371 个字

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6169

题意:给了区间L,R,求[L,R]区间所有满足其最小质数因子为k的数的和。

解法:

2017多校第9场 HDU 6169  Senior PanⅡ 数论,DP,爆搜

我参考的这篇blog。http://blog.csdn.net/wubaizhe/article/details/77484454#cpp

2017多校第9场 HDU 6169  Senior PanⅡ 数论,DP,爆搜

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL mod = 1e9+7;
const int Mx = 320000;
const int A = Mx + 10;
const int B = 1e4+10;
const int C = 1e2+10;
const LL inv = 500000004;
LL dp[B][C];
//dp[i][j]表示1~i 这 i个数被前j个素数筛过以后剩下的数的和。
//dp[i][j]=dp[i][j−1]−pri[j]∗dp[i/pri[j]][j−1]
int prime[A],tot;
bool isprime[A];
void predeal(){
tot=0;
memset(isprime, 1, sizeof(isprime));
for(LL i=2; i<A; i++){
if(isprime[i]){
for(LL j=i+i; j<A; j+=i){
isprime[j]=0;
}
}
}
for(int i=2; i<A; i++){
if(isprime[i]) prime[++tot] = i;
}
for(int i=1; i<B; i++)
{
dp[i][0] = 1LL*i*(i+1)/2%mod;
for(int j=1; j<C; j++)
{
dp[i][j] = (dp[i][j-1] - prime[j]*dp[i/prime[j]][j-1]%mod + mod)%mod;
}
}
}
LL dfs(LL n, LL m)
{
if(n<=1) return n;
if(!m) return n%mod*(n%mod+1)%mod*inv%mod;
if(n<B&&m<C) return dp[n][m];
if(prime[m]>n) return 1;
return (dfs(n,m-1)-prime[m]*dfs(n/prime[m],m-1)%mod+mod)%mod;
}
bool check(LL x){
for(LL i=2; i*i<=x; i++){
if(x%i==0){
return false;
}
}
return true;
}
int main()
{
predeal();
int T,ks=0;
scanf("%d", &T);
while(T--)
{
LL L,R,k;
scanf("%lld%lld%lld", &L,&R,&k);
printf("Case #%d: ", ++ks);
if(!check(k)){
puts("0");
}
else if(k>Mx){
if(L<=k&&k<=R) printf("%lld\n",k%mod);
else puts("0");
}
else{
int now=0;
while(prime[now+1]<k) now++;
LL ans = (dfs(R/k,now)*k%mod-dfs((L-1)/k,now)*k%mod + mod)%mod;
if(ans < 0) ans+=mod;
printf("%lld\n", ans);
}
}
return 0;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,910
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,435
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,250
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,061
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,693
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,731