Loading

洛谷2054-AHOI洗牌

2054-AHOI洗牌

考虑

\[a_{i+1}=2a_{i}(a_{i}<=n/2),a_{i+1}=2a_{i}-(n+1)(a_{i}>n/2) \]

转化合并成

\[a_{i+1}=2a_{i}(mod(n+1))-->a_{m}=l=2^ma_{0}(mod(n+1)),求a_{0} \]

使用逆元求解即可。注意本题会爆long long,应使用快速乘。

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsinged long long
#define ios ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl '\n'
#define debugg(x) cout<<#x<<"="<<x<<endl;
#define debug1(x,y) cout<<#x<<' '<<#y<<' '<<x<<' '<<y<<endl;
#define debug cout<<endl<<"*********"<<endl;
#define itn int
#define pii pair<int,int>
#define mid ((all[now].lo+all[now].ro)/2)
#define mod (ll)(n+1)
void fre(){
    freopen("test.in","r",stdin);
    freopen("test.out","w",stdout);
}
void fc(){fclose(stdin);fclose(stdout);}
const int maxn=1e5+10;
const ll inf=1e16;
ll n,m,l,x1,y;
inline ll ksc(ll a,ll b){
    ll ans1=0;
    while(b>0){
        if(b%2) ans1=(ans1+a)%mod;
        a=(a+a)%mod;
        b/=2;
    }
    return ans1;
}
ll ksm(ll now,ll ci){
    ll ans1=1;
    while(ci>0){
        if(ci%2==1) ans1=ksc(ans1,now)%mod;
        ci/=2;now=ksc(now,now)%mod;
    }
    return ans1;
}
void exgcd(ll a,ll b){
    if(!b){x1=1;y=0;return;}
    exgcd(b,a%b);
    ll tmp=x1;x1=y;y=tmp-a/b*y;
}
int main(){
    ios;fre();
    cin>>n>>m>>l;
    ll now1=ksm(2,m);
    exgcd(now1,mod);
    x1=x1%mod+mod;
    cout<<(ll)(ksc(x1%mod,l%mod)%mod)<<endl;
    return 0;
}
posted @ 2021-03-19 20:56  14long  阅读(46)  评论(0)    收藏  举报