[luogu2054 AHOI2005] 洗牌 (數論)
阿新 • • 發佈:2018-10-04
ring sign math org efi 部分 string -a pro
傳送門
Solution
我們考慮每一步牌的變化:
- 前半部分的牌位置*2
- 後半部分的牌位置*2-n-1
那麽我們可以看做是\(x\times 2^m\equiv l \pmod n\)
於是求個逆元就好了
Code
#include <cmath> #include <cstdio> #include <cstring> #include <cstdlib> #include <iostream> #include <algorithm> #define F(i,a,b) for(register int i=(a);i<=(b);i++) using namespace std; typedef long long LL; typedef unsigned long long ull; inline LL read() { LL x=0,f=1;char c=getchar(); while(!isdigit(c)) {if(c=='-')f=-f;c=getchar();} while(isdigit(c)) x=(x<<1)+(x<<3)+c-48,c=getchar(); return x*f; } LL MOD,m,l; LL exgcd(LL a,LL b,LL &x,LL &y) { if(!b) {x=1;y=0;return a;} LL d=exgcd(b,a%b,x,y),t=x;x=y;y=t-a/b*y; return d; } LL qpow(LL a,LL b) { LL t=1; while(b) { if(b&1) t=t*a%MOD; a=a*a%MOD; b>>=1; } return t; } LL inv(LL a) { LL x,y;exgcd(a,MOD,x,y); return (x%MOD+MOD)%MOD; } int main() { MOD=read()+1,m=read(),l=read(); printf("%lld",l*inv(qpow(2,m))%MOD); return 0; }
[luogu2054 AHOI2005] 洗牌 (數論)