# 【BZOJ-1965】SHUFFLE 洗牌 快速幂 + 拓展欧几里德

## 1965: [Ahoi2005]SHUFFLE 洗牌

Time Limit: 3 Sec  Memory Limit: 64 MB
Submit: 541  Solved: 326
[Submit][Status][Discuss]

6 2 3

6

Day1

## Code

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cstdio>
using namespace std;
{
long long x=0,f=1; char ch=getchar();
while (ch<'0' || ch>'9') {if (ch=='-') f=-1; ch=getchar();}
while (ch>='0' && ch<='9') {x=x*10+ch-'0'; ch=getchar();}
return x*f;
}
long long n,m,l;
long long quick_pow(long long x,long long y,long long p)
{
long long re=1;
for (int i=y; i; i>>=1,x=x*x%p)
if (i&1) re=re*x%p;
return re;
}
long long exgcd(long long a,long long b,long long &x,long long &y)
{
if (b==0) {x=1; y=0; return a;}
long long gcd=exgcd(b,a%b,x,y);
long long tmp=x; x=y; y=tmp-a/b*y;
return gcd;
}
long long Gcd(long long a,long long b)
{
if (b==0) return a; return Gcd(b,a%b);
}
int main()
{
m=quick_pow(2,m,n+1); n++;
long long gcd=Gcd(n,m); n/=gcd; m/=gcd; l/=gcd;
long long x,y; gcd=exgcd(m,n,x,y);
x=x*l%n; while (x<0) x+=n;
printf("%lld\n",x);
return 0;
}

——It's a lonely path. Don't make it any lonelier than it has to be.
posted @ 2016-04-03 11:12  DaD3zZ  阅读(203)  评论(0编辑  收藏  举报