zoukankan      html  css  js  c++  java
  • [bzoj2242][Sdoi2011]计算器_exgcd_BSGS

    计算器 bzoj-2242 Sdoi-2011

    题目大意:裸题,支持快速幂、扩展gcd、拔山盖世

    注释:所有数据保证int,10组数据。

    想法:裸题,就是注意一下exgcd别敲错... ...

    最后,附上丑陋的代码... ...

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    #include <map>
    using namespace std;
    #define LL long long
    map<LL,int> mp;
    LL qp(LL x,LL y,LL mod)
    {
        LL re=1;
        while(y)
    	{
            if(y&1ll)re=re*x%mod;
            x=x*x%mod;
            y>>=1ll;
        }
        return re;
    }
    void exgcd(LL a,LL b,LL &x,LL &y,LL &p)
    {
        if(!b){x=1;y=0;p=a;return ;}
        exgcd(b,a%b,y,x,p);
        y-=(a/b)*x;
    }
    LL BSGS(LL n,LL a,LL b)
    {
        if(n==1)if(!b)return a!=1; else return -1;
        if(b==1)if(a)return 0; else return -1;
        if(a%n==0)if(!b)return 1; else return -1;
        LL m=ceil(sqrt(n)),d=1,base=1;
        mp.clear();
        for(int i=0;i<m;i++)
        {
            if(!mp.count(base))mp[base]=i;
            base=(base*a)%n;
        }
        for(int i=0;i<m;i++)
        {
            LL x,y,s;
            exgcd(d,n,x,y,s);
            x=(x*b%n+n)%n;
            if(mp.count(x))return i*m+mp[x];
            d=(d*base)%n;
        }
        return -1;
    }
    int main()
    {
        int t,k;
        scanf("%d%d",&t,&k);
        int i;
        LL a,b,n,x,y,p;
        for(i=1;i<=t;i++)
    	{
            scanf("%lld%lld%lld",&a,&b,&n);
            if(k==1)
    		{
                printf("%lld
    ",qp(a,b,n));
            }
    		else if(k==2)
    		{
                exgcd(a,n,x,y,p);
                if(b%p)
    			{
                    puts("Orz, I cannot find x!");continue;
                }
                x=(x*(b/p)%n+n)%n;
                printf("%lld
    ",x);
            }
    		else if(k==3)
    		{
                LL x=BSGS(n,a,b);
                if(x==-1)puts("Orz, I cannot find x!");
                else printf("%lld
    ",x);
            }
        }
    }
    

     小结:裸题而已,小结啥...

  • 相关阅读:
    ElasticSearch 2 (23)
    ElasticSearch 2 (22)
    微信小程序框架模板部署:mpvue2.x+typescript+webpack3.x
    mpvue添加对scss的支持
    mpvue 封装axios请求方法
    Vue中qs插件的使用
    在微信小程序中使用less/sass
    微信小程序封装request请求
    VSCode --tsc : 无法加载文件
    Vue项目中的RSA加解密
  • 原文地址:https://www.cnblogs.com/ShuraK/p/9344575.html
Copyright © 2011-2022 走看看