BZOJ1951:[Sdoi2010]古代猪文 (扩展欧拉定理+Lucas定理+CRT)

题目传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1951


题目分析:又是一道把几个数论算法强行拼起来的题目。

题面要求的其实是 gd|nCdnmodp g ∑ d | n C n d mod p 。用扩展欧拉定理可以变成 g(d|nCdn)modϕ(p)+ϕ(p)modp g ( ∑ d | n C n d ) mod ϕ ( p ) + ϕ ( p ) mod p 。写个程序发现 p=999911659 p = 999911659 是质数,然后写个质因数分解发现 p1=999911658=23467935617 p − 1 = 999911658 = 2 ∗ 3 ∗ 4679 ∗ 35617 。于是对每个质数用Lucas定理求,然后CRT合并就好了。

刚刚逛网页的时候忽然发现了某dalao的blog,然后感觉自己还是太弱了。


CODE:

#include<iostream>
#include<string>
#include<cstring>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<stdio.h>
#include<algorithm>
using namespace std;

const int maxn=50000;
const int M[4]={2,3,4679,35617};

int fac[4][maxn];
int nfac[4][maxn];

int p=M[0]*M[1]*M[2]*M[3];
int v[4];
int g,n;

int Pow(int x,int y,int z)
{
    if (!y) return 1;
    long long temp=Pow(x,y>>1,z);
    temp=temp*temp%z;
    if (y&1) temp=temp*x%z;
    return temp;
}

int C(int n,int m,int x)
{
    if ( n<M[x] && m<M[x] )
        return (long long)fac[x][n]*nfac[x][m]%M[x]*nfac[x][n-m]%M[x];
    return (long long)C(n/M[x],m/M[x],x)*C(n%M[x],m%M[x],x)%M[x];
}

int Work(int d)
{
    for (int i=0; i<4; i++) v[i]=C(n,d,i);
    int ans=0;
    for (int i=0; i<4; i++)
        ans=(ans+ (long long)v[i]*(p/M[i])%p*Pow(p/M[i],M[i]-2,M[i])%p )%p;
    return ans;
}

int main()
{
    //freopen("1951.in","r",stdin);
    //freopen("1951.out","w",stdout);

    for (int i=0; i<4; i++)
    {
        int x=M[i];
        fac[i][0]=1;
        for (int j=1; j<x; j++) fac[i][j]=fac[i][j-1]*j%x;
        for (int j=0; j<x; j++) nfac[i][j]=Pow(fac[i][j],x-2,x);
    }

    scanf("%d%d",&n,&g);
    int sn=(int)floor( sqrt( (double)n )+1e-4 );
    int ans=0;
    for (int i=1; i<=sn; i++) if (n%i==0)
    {
        ans=(ans+ Work(i) )%p;
        if (i!=n/i) ans=(ans+ Work(n/i) )%p;
    }
    ans+=p;
    ans=Pow(g,ans,p+1);
    printf("%d\n",ans);

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值