数论——bzoj3560: DZY Loves Math V

http://www.lydsy.com/JudgeOnline/problem.php?id=3560
我要好好练习数论了,今日见到数论一点思路都没有,唉;
公示推倒看popoqqq的好了,写的贼好;
http://blog.csdn.net/popoqqq/article/details/42739963
当然队欧拉函数的理解是基础;
代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#define Ll long long
using namespace std;
struct cs{int x,y;}q[1000000];
int a[100005];
int n,m,top,x,last,mo=1e9+7;
Ll f[10000],ans=1;
void make(int x){
    for(int i=2;i*i<=x;i++)
        if(x%i==0){
            q[++top].x=i;
            for(;x%i==0;x/=i)q[top].y++;
        }
    if(x!=1)q[++top].x=x,q[top].y=1;
}
bool cmp(cs a,cs b){if(a.x!=b.x)return a.x<b.x;return a.y<b.y;}
Ll ksm(Ll x,Ll y){
    Ll ans=1;
    for(;y;y>>=1,x=x*x%mo)if(y&1)ans=ans*x%mo;
    return ans;
}
Ll find(int x,int y){
    f[0]=1; Ll temp=1;
    for(int i=1;i<=q[y].y;i++)f[i]=f[i-1]*q[x].x%mo;
    for(int i=1;i<=q[y].y;i++)f[i]=(f[i]+f[i-1])%mo;
    for(int i=x;i<=y;i++)temp=temp*f[q[i].y]%mo;
    temp=(temp-1)*ksm(q[x].x,mo-2)%mo*(q[x].x-1)+1;
    return temp%mo;
}
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)scanf("%d",&x),make(x);
    sort(q+1,q+top+1,cmp);
    for(int i=1;i<=top;i++)
        if(q[i].x!=q[i+1].x){
            ans=ans*find(last+1,i)%mo;
            last=i;
        }
    printf("%lld",ans);
}

然后我的同学还有一种写法,就是吧上 面的公示化开用递推;我不会啊

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<queue>
#include<string>
#include<map>
#include<cstring>
#include<ctime>
#include<vector>
#define inf 1e9
#define ll long long
#define For(i,j,k) for(ll i=j;i<=k;i++)
#define Dow(i,j,k) for(ll i=k;i>=j;i--)
using namespace std;
int n;
ll ans=1,zy[1000001],cnt[1000001],tot;
int tim[1000001],tmp[1000001];
ll mo=1e9+7;
void get(int &x)
{
    char c = getchar(); x = 0;
    while(c < '0' || c > '9') c = getchar();
    while(c <= '9' && c >= '0') x = x*10+c-48, c = getchar();
}
int main()
{
    freopen("phi.in","r",stdin);
    freopen("phi.out","w",stdout);
    get(n);
    For(i,1,1000000)    zy[i]=tim[i]=tmp[i]=1;  
    For(i,1,n)
    {
        int x;
        get(x);
        int t=sqrt(x);
        for (int y=2;y*y<=x;y++)
        {
            while(x%y==0)
            {
                if(tim[y]!=i)   tim[y]=i,tmp[y]=zy[y];  //cout<<zy[y]*y<<' '<<tmp[y]-1<<' '<<y<<endl;
                zy[y]=(zy[y]*y+tmp[y]-1)%mo;
                x/=y;   
            }   
        }
        tim[x]=i,tmp[x]=zy[x];
        zy[x]=(zy[x]*x+tmp[x]-1)%mo;

    }
    For(i,1,1000000)    ans=(ans*zy[i])%mo;
    printf("%d",ans);
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值