2017 Multi-University Training Contest - Team 4 :1003&hdu6069、Counting Divisors

题目:

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, given l,r and k , your task is to calculate the following thing :

(i=lrd(ik))mod998244353

 

Input
The first line of the input contains an integer T(1T15) , denoting the number of test cases.

In each test case, there are 3 integers l,r,k(1lr1012,rl106,1k107) .
 

Output
For each test case, print a single line containing an integer, denoting the answer.
 

Sample Input
  
  
3 1 5 1 1 10 2 1 100 3
 

Sample Output
  
  
10 48 2302

题意:求区间[l,r]中的元素i^k的因子数之和。

思路:由于数据过大,所以暴力是行不通的!!!根据定理(貌似叫约数定理):n=p1^a1×p2^a2×p3^a3*…*pm^am,n的因子数的个数就是(a1+1)(a2+1)(a3+1)…(am+1)。所以可以推理出n^k的因子数为(k*a1+1)(k*a2+1)(k*a3+1)…(k*am+1)。由于r最大值为1e12,所以它的质因子最大不超过1e6。把[1,1e6]的素数利用素数筛法求出。枚举每个素数在区间内的倍数,可以跳着枚举,计算出每个数对应的因子个数。若区间中的数不断模除因子后的最終结果不是1,则说明这个数本身是素数,素数的因子个数就等于枚举的因子个数*(k+1)累乘起来。。。吐槽:不知道为什么一直TLE、RE,然后去看别人的题解没找到错误,替换两行相同的代码后就AC了,真的醉了,玄学啊再见再见再见附上TLE代码一份,望大佬指教有何不同

CODE:

TLE:

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
#define mod 998244353
#define maxn 1000001
bool isprime[maxn];
int cnt;
LL prime[maxn],f[maxn],num[maxn],l,r,k;
void Prime(){
        cnt=0;
        memset(isprime,true,sizeof(isprime));
        isprime[1]=false;
        for(int i=2;i<maxn;i++){
                if(isprime[i]){
                        prime[cnt++]=i;
                        for(int j=2;j*i<maxn;j++) isprime[i*j]=false;
                }
        }
}
int main()
{
        Prime();
        int t;
        scanf("%d",&t);
        while(t--){
                scanf("%lld%lld%lld",&l,&r,&k);
                for(int i=0;i<=r-l;i++) f[i]=i+l,num[i]=1;
                for(int i=0;i<cnt;i++){
                        LL st=(l/prime[i]+(l%prime[i]?1:0))*prime[i];
                        for(LL j=st;j<=r;j+=prime[i]){
                                LL len=0;
                                /*while(f[j-1]%prime[i]==0){
                                        len++;
                                        f[j-1]/=prime[i];
                                }
                                num[j-1]=(num[j-1]*((len*k+1)%mod))%mod;*/有毒的代码,换第一句RE,全换AC
                        }
                }
                LL sum=0;
                for(int i=0;i<=r-l;i++){
                        if(f[i]!=1) num[i]=(num[i]*(k+1))%mod;
                        sum=(sum+num[i])%mod;
                }
                printf("%lld\n",sum);
        }
        return 0;
}


 
 

AC:

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
#define mod 998244353
#define maxn 1000001
bool isprime[maxn];
int cnt;
LL prime[maxn],f[maxn],num[maxn],l,r,k;
void Prime(){
        cnt=0;
        memset(isprime,true,sizeof(isprime));
        isprime[1]=false;
        for(int i=2;i<maxn;i++){
                if(isprime[i]){
                        prime[cnt++]=i;
                        for(int j=2;j*i<maxn;j++) isprime[i*j]=false;
                }
        }
}
int main()
{
        Prime();
        int t;
        scanf("%d",&t);
        while(t--){
                scanf("%lld%lld%lld",&l,&r,&k);
                for(int i=0;i<=r-l;i++) f[i]=i+l,num[i]=1;
                for(int i=0;i<cnt;i++){
                        LL st=(l/prime[i]+(l%prime[i]?1:0))*prime[i];
                        for(LL j=st;j<=r;j+=prime[i]){
                                LL len=0;
                                while(f[j-l]%prime[i]==0) len++,f[j-l]/=prime[i];
                                num[j-l]=(num[j-l]*((len*k+1)%mod))%mod;
                        }
                }
                LL sum=0;
                for(int i=0;i<=r-l;i++){
                        if(f[i]!=1) num[i]=(num[i]*(k+1))%mod;
                        sum=(sum+num[i])%mod;
                }
                printf("%lld\n",sum);
        }
        return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值