[luogu2054 AHOI2005] 洗牌 (数论)

传送门

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;
}
posted @ 2018-10-03 20:48  Menteur_hxy  阅读(163)  评论(0编辑  收藏  举报