【BZOJ 3560】【数论】DZY Loves Math V

本文解析了一道涉及欧拉函数的数学计算题,通过质因数分解和前缀和预处理的方法,给出了详细的解题思路及C++实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目大意:

给定n个正整数a1,a2,…,an,求下方式子的值(答案模10^9+7)。
来自BZOJ

题目解析:

首先,因为欧拉函数是积性函数,对于每一个ϕ(i1i2i3...in)我们可以将其分解质因数,对于质因数pbi表示ai中质因数p的数量。
方程变成

Πp(i1=0b1i2=0b2...in=0bnpnj=1ij1)p1p+1

本来欧拉函数求筛法时某一个质数第一次出现乘p1,其他的乘p,这里我们把p1先换成p,统计完再换回来。至于那个减一是因为ϕ(1)被统计了,故减去。
再次变换就可以得到:

Πp(ΠNi=1j=0bi(pj)1)p1p+1

于是我们只需要预处理出pj的前缀和,然后枚举因数统计即可。
/**************************************************************
    Problem: 3560
    User: szpszp
    Language: C++
    Result: Accepted
    Time:4344 ms
    Memory:15356 kb
****************************************************************/

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;

#define MAXN 100000
#define MAXM
#define INF 0x3f3f3f3f
typedef long long int LL;
const LL MOD = 1000000007ll;

template<class T>
void Read(T &x){
    x=0;char c=getchar();bool flag=0;
    while(c<'0'||'9'<c){if(c=='-')flag=1;c=getchar();}
    while('0'<=c&&c<='9'){x=x*10+c-'0';c=getchar();}
    if(flag)x=-x;
}

struct node{
    LL num,cnt;
    bool operator < (const node &a)const{
        if(num!=a.num)return num<a.num;
        else return cnt<a.cnt;
    }
};

int N;
node tmp[MAXN*9+10];
int len;

void Get_Divisor(int x){
    for(int i=2;i*i<=x;++i)
        if(x%i==0){
            tmp[++len].num=i;
            do{
                ++tmp[len].cnt;
                x/=i;
            }while(x%i==0);
        }
    if(x!=1)tmp[++len].num=x,tmp[len].cnt=1;
}

LL ksm(LL a,LL p){
    LL rn=1;
    while(p){
        if(p&1)rn=(rn*a)%MOD;
        a=(a*a)%MOD;
        p>>=1;
    }
    return rn;
}

LL sum[30];
LL GetAns(int l,int r){
    LL p=tmp[l].num;
    LL rn=1;
    sum[0]=1;
    for(int i=1;i<=tmp[r].cnt;++i)
        sum[i]=sum[i-1]*p%MOD;
    for(int i=1;i<=tmp[r].cnt;++i)
        sum[i]=(sum[i]+sum[i-1])%MOD;
    for(int i=l;i<=r;++i)
        rn=(rn*sum[tmp[i].cnt])%MOD;

    --rn;
    rn=(rn*ksm(p,MOD-2))%MOD;
    rn=(rn*(p-1)+1)%MOD;

    return rn;
}

int main(){
    Read(N);
    int x;
    for(int i=1;i<=N;++i){
        Read(x);
        Get_Divisor(x);
    }

    sort(tmp+1,tmp+len+1);

    LL ans=1;
    int last=1;
    for(int i=1;i<=len;++i){
        if(i==len||tmp[i].num!=tmp[i+1].num){
            ans=(ans*GetAns(last,i))%MOD;
            last=i+1;
        }
    }

    printf("%lld\n",(ans%MOD+MOD)%MOD);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值