zoukankan      html  css  js  c++  java
  • P2054 [AHOI2005]洗牌

    P2054 [AHOI2005]洗牌
    https://www.luogu.org/problem/P2054
    手推一下小数据,会发现,当前编号是x的拍,再进行m次洗牌之后的位置为(2^m)*x%(n+1);
    所以我们要解这个方程组--> (2^m)*x%(n+1)==l
    x=l*((2^m)^-1)%(n+1);
    套扩欧求逆元就完事了,但是wa了两个点,改成__int128就过了

    #include <iostream>
    #include <cstdio>
    #include <queue>
    #include <algorithm>
    #include <cmath>
    #include <cstring>
    #define inf 2147483647
    #define N 1000010
    #define p(a) putchar(a)
    #define For(i,a,b) for(__int128 i=a;i<=b;++i)
    //by war
    //2019.8.7
    using namespace std;
    __int128 n,m,l,x,y;
    void in(__int128 &x){
        __int128 y=1;char c=getchar();x=0;
        while(c<'0'||c>'9'){if(c=='-')y=-1;c=getchar();}
        while(c<='9'&&c>='0'){ x=(x<<1)+(x<<3)+c-'0';c=getchar();}
        x*=y;
    }
    void o(__int128 x){
        if(x<0){p('-');x=-x;}
        if(x>9)o(x/10);
        p(x%10+'0');
    }
    __int128 ksm(__int128 a,__int128 b){
        __int128 r=1;
        while(b>0){
            if(b&1)
                r=r*a%(n+1);
            a=a*a%(n+1);
            b>>=1;
        }
        return r;
    }
    
    void exgcd(__int128 a,__int128 b,__int128 &x,__int128 &y){
        if(!b){
            x=1;y=0;
            return;
        }
        exgcd(b,a%b,y,x);
        y-=a/b*x;
    }
    
    signed main(){
        in(n);in(m);in(l);
        l%=n+1;
        exgcd(ksm(2,m),n+1,x,y);
        x=(x%(n+1)+n+1)%(n+1);
        o(x*l%(n+1));
        return 0;
    }

    __int128有的OJ不支持,这是这个题的慢速乘版本

    #include <iostream>
    #include <cstdio>
    #include <queue>
    #include <algorithm>
    #include <cmath>
    #include <cstring>
    #define inf 2147483647
    #define N 1000010
    #define p(a) putchar(a)
    #define For(i,a,b) for(long long i=a;i<=b;++i)
    //by war
    //2019.8.7
    using namespace std;
    long long n,m,l,x,y;
    void in(long long &x){
        long long y=1;char c=getchar();x=0;
        while(c<'0'||c>'9'){if(c=='-')y=-1;c=getchar();}
        while(c<='9'&&c>='0'){ x=(x<<1)+(x<<3)+c-'0';c=getchar();}
        x*=y;
    }
    void o(long long x){
        if(x<0){p('-');x=-x;}
        if(x>9)o(x/10);
        p(x%10+'0');
    }
    
    long long msc(long long a,long long b){
        long long r=0;
        while(b>0){
            if(b&1)
                r=(r+a)%(n+1);
            a=(a<<1)%(n+1);
            b>>=1;
        }
        return r;
    }
    
    long long mscc(long long a,long long b){
        long long r=0,y=1;
        if((a^b)<0)  y*=-1;
        a=abs(a);b=abs(b);
        while(b>0){
            if(b&1)
                r+=a;
            a<<=1;
            b>>=1;
        }
        return r*y;
    }
    
    long long ksm(long long a,long long b){
        long long r=1;
        while(b>0){
            if(b&1)
                r=msc(r,a);
            a=msc(a,a);
            b>>=1;
        }
        return r;
    }
    
    void exgcd(long long a,long long b,long long &x,long long &y){
        if(!b){
            x=1;y=0;
            return;
        }
        exgcd(b,a%b,y,x);
        y-=mscc(a/b,x);
    }
    
    signed main(){
        in(n);in(m);in(l);
        l%=n+1;
        exgcd(ksm(2,m),n+1,x,y);
        x=(x%(n+1)+n+1)%(n+1);
        o(msc(x,l));
        return 0;
    }
  • 相关阅读:
    尽量采用as操作符而不是旧式C风格做强制类型转换
    尽量使用条件属性(Conditional Attribute)而不是#if/#endif预处理
    C#跟踪和调试程序-Debug类使用
    C#使用ConditionalAttribute特性来实现代码调试
    微软认知服务:QnA Maker使用示例
    PHP使用微软认知服务Face API
    微软认知服务识别名人和地标
    基于apache httpclient 调用Face++ API
    认知服务调用如何使用图片的DataURL
    Microsoft Azure Storage Exployer使用指南
  • 原文地址:https://www.cnblogs.com/war1111/p/11317301.html
Copyright © 2011-2022 走看看