hdu 4335 What is N?

题目:4335

思路:对n!分成三部分,小于euler(mod)的部分,剩下的,根据能否整除euler(mod),分成循环节部分和剩余的部分(暴力)


参考了这里, here


#include <cstdio>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <map>
using namespace std;
typedef unsigned __int64 LL;
LL mod,b,M;
LL euler(LL x)
{
    LL i,res=x;
    for(int i=2;i*i<=x;i++)
        if(x%i==0)
        {
            res=res/i*(i-1);
            while(x%i==0)
                x/=i;
        }
    if(x>1)
        res=res/x*(x-1);
    return res;
}
LL Pow(LL a,LL b)
{
    LL ans=1;
    while(b)
    {
        if(b&1)
        {
            b--;
            ans=(ans*a)%mod;
        }
        else
        {
            b/=2;
            a=(a*a)%mod;
        }
    }
    return ans;
}
int main()
{
    int t;
    cin>>t;
    for(int cases=1;cases<=t;cases++)
    {
        cin>>b>>mod>>M;
        cout<<"Case #"<<cases<<": ";
        LL eul=euler(mod);

        if(mod==1)
        {
            if(b==0)
            {
                if(M==18446744073709551615LLU)
                    cout<<"18446744073709551616"<<endl;
                else
                    cout<<M+1<<endl;
            }
            else
                cout<<0<<endl;
        }
        else
        {
            LL cnt=1,pnt=0;
            LL ans=0;
            LL fac=1;
            for(pnt=0;pnt<=M;pnt++)
            {
                if(fac>=eul)
                    break;
                if(Pow(pnt,fac)==b)
                    ans++;
                fac*=pnt+1;
            }
            fac%=eul;
            for(;pnt<=M;pnt++)
            {
                if(fac==0)
                    break;
                if(Pow(pnt,fac+eul)==b)
                    ans++;
                fac=fac*(pnt+1)%eul;
            }
            if(pnt<=M)
            {
                LL tmp=0;
                for(LL i=0;i<mod;i++)
                    if(Pow(pnt+i,eul)==b)
                        tmp++;
                LL miao=(M-pnt+1)/mod;
                ans+=miao*tmp;
                miao=(M-pnt+1)-miao*mod;
                for(LL i=0;i<miao;i++)
                    if(Pow(pnt+i,eul)==b)
                        ans++;
            }
            cout<<ans<<endl;
        }
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值