过度工整代码在线求调

P1593 因子和

czr2012 @ 2023-07-30 12:47:58

#82

#include <bits/stdc++.h>
#include <iostream>
using namespace std;
#define int long long
#define il inline
#define re register
#define endl '\n'

const int maxn=5e7+1;
const int mod=9901;
int pow(int a,int b)
{
    int res=1;
    while(b>0)
    {
        if(b&1)
        {
            res=(res*a)%mod;
        }
        a=(a*a)%mod;
        b>>=1;
    }
    return res;
}
int a,b;
vector<int>zs;
bool flag[maxn];
int cnt[maxn];
int inv[maxn];
int ans=1;
vector<pair<int,int>>v;

void work()
{
    for(int i:zs)
    {
        if(a==1)
        {
            break;
        }
        while(a%i==0)
        {
            cnt[i]++;
            a/=i;
        }
        cnt[i]*=b;
        if(cnt[i]!=0)
        {
            v.push_back({i,cnt[i]});
        }
    }
    bool flag2=false;
    for(pair<int,int>i:v)
    {
        ans*=(pow(i.first,i.second+1)-1)%mod*inv[(i.first-1)%mod]%mod;
        ans%=mod;
        flag2=true;
    }
    cout<<(ans%mod+mod)%mod<<endl;
}

void init()
{
    zs.push_back(2);
    memset(flag,true,sizeof(flag));
    for(int i=3;i*i<=maxn;i++)
    {
        if(flag[i])
        {
            zs.push_back(i);
        }
        for(int j:zs)
        {
            if(i*i*j*j>maxn)
            {
                break;
            }
            flag[i*j]=false;
        }
    }
    inv[1]=1;
    for(int i=2;i<mod;i++)
    {
        inv[i]=(mod-mod/i)*inv[mod%i]%mod;
    }
    cin>>a>>b;
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    init();
    work();
    return 0;
}

by czr2012 @ 2023-07-30 13:33:40

改成

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define il inline
#define re register
#define endl '\n'

const int maxn=5e7+1;
const int mod=9901;
int pow(int a,int b)
{
    int res=1;
    while(b>0)
    {
        if(b&1)
        {
            res=(res*a)%mod;
        }
        a=(a*a)%mod;
        b>>=1;
    }
    return res;
}
int a,b;
vector<int>zs;
bool flag[maxn];
int cnt[maxn];
int inv[maxn];
int ans=1;
vector<pair<int,int>>v;

void work()
{
    for(int i:zs)
    {
        if(a==1)
        {
            break;
        }
        while(a%i==0)
        {
            cnt[i]++;
            a/=i;
        }
        cnt[i]*=b;
        if(cnt[i]!=0)
        {
            v.push_back({i,cnt[i]});
        }
    }
    if(a!=1)
    {
        v.push_back({a,b});
    }
    bool flag2=false;
    for(pair<int,int>i:v)
    {
        if(i.first%mod==1)
        {
            ans*=(i.second+1)%mod;
        }
        else
        {
            ans*=(pow(i.first,i.second+1)-1)%mod*inv[(i.first-1)%mod]%mod;
        }
        ans%=mod;
        flag2=true;
    }
    cout<<(ans%mod+mod)%mod<<endl;
}

void init()
{
    zs.push_back(2);
    memset(flag,true,sizeof(flag));
    for(int i=3;i*i<=maxn;i++)
    {
        if(flag[i])
        {
            zs.push_back(i);
        }
        for(int j:zs)
        {
            if(i*i*j*j>maxn)
            {
                break;
            }
            flag[i*j]=false;
        }
    }
    inv[1]=1;
    for(int i=2;i<mod;i++)
    {
        inv[i]=(mod-mod/i)*inv[mod%i]%mod;
    }
    cin>>a>>b;
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    init();
    work();
    return 0;
}

by czr2012 @ 2023-07-30 13:34:41

@czr2012 谢谢大佬,关注了。


by _buzhidao_ @ 2023-07-30 13:54:10

@czr2012 《自导自演》


by czr2012 @ 2023-07-31 00:18:03

导演: \ \ 无(无导自演)

演员组: \ \ czr2012 饰 czr2012 \ czr2012 饰 czr2012


by czr2012 @ 2023-07-31 00:18:15

@buzhidao


by wangyutian578 @ 2023-08-05 19:31:23

hhh


|