hdu6069 Counting Divisors(数学)

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

题意:

d(xk)xk121,2,3,4,6,126d(12)=6i=lrd(ik)

官方题解:

n=pc11pc22...pcmmd(nk)=(kc1+1)(kc2+1)...(kcm+1)

rp[l,r]pr01

总结:

O(n)使

O(r)[1,r][l,r]

代码:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>

using namespace std;

typedef long long ll;

const int maxn = 1e6 + 100;
const int mod = 998244353;
bool valid[maxn];
int ans[maxn], tot;
ll mp[maxn], d[maxn];

void getPrime(int n, int &tot, int ans[]){
    tot = 0;
    memset(valid, true, sizeof(valid));
    valid[0] = valid[1] = false;
    for(int i=2; i<=n; ++i){
        if(valid[i]){
            ans[++tot] = i;
        }
        for(int j=1; ((j<=tot)&&(i*ans[j]<=n)); ++j){
            valid[i*ans[j]] = false;
            if(i%ans[j]==0) break;
        }
    }
}

ll solve(ll l, ll r, ll k){
    for(int i=1; i<=tot; ++i){
        ll idx = l / ans[i] + (l%ans[i]==0?0:1);
        idx = idx * ans[i];//第一个大于等于l并且是素数ans[i]的倍数的数

        if(idx > r) break;

        while(idx <= r){
            ll cnt = 0;
            while(mp[idx-l] % ans[i] == 0){
                mp[idx-l] /= ans[i];
                ++cnt;
            }
            d[idx-l] = d[idx-l] * ((cnt*k+1) % mod) % mod;
            idx += ans[i];
        }
    }
    ll ret = 0;
    for(ll i=0; i<=r-l; ++i){
        if(mp[i]==1) 
            ret += d[i];
        else
            ret += d[i] * (k+1);
        ret %= mod;
    }
    return ret;
}

int main(){
    ll t, l, r, k;
    ios::sync_with_stdio(false);
    cin>>t;
    getPrime(maxn-5, tot, ans);
    while(t--){
        cin>>l>>r>>k;
        for(ll i=l; i<=r; ++i){
            d[i-l] = 1;
            mp[i-l] = i;
        }
        cout<<solve(l, r, k)<<endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值