现在的位置: 首页 > 综合 > 正文

【逆元】【bzoj 1965】: [Ahoi2005]SHUFFLE 洗牌

2017年04月21日 ⁄ 综合 ⁄ 共 2785字 ⁄ 字号 评论关闭

http://www.lydsy.com/JudgeOnline/problem.php?id=1965


x*2^m==l (mod n+1)

然后逆元


#define _TEST _TEST
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
/************************************************
Code By willinglive    Blog:http://willinglive.cf
************************************************/
#define rep(i,l,r) for(int i=(l),___t=(r);i<=___t;i++)
#define per(i,r,l) for(int i=(r),___t=(l);i>=___t;i--)
#define MS(arr,x) memset(arr,x,sizeof(arr))
#define LL long long
#define INE(i,u,e) for(int i=head[u];~i;i=e[i].next)
inline const int read()
{int r=0,k=1;char c=getchar();for(;c<'0'||c>'9';c=getchar())if(c=='-')k=-1;
for(;c>='0'&&c<='9';c=getchar())r=r*10+c-'0';return k*r;}
/////////////////////////////////////////////////
LL mod;
LL n,m,l;
/////////////////////////////////////////////////
inline LL mul_mod(LL a,LL b,LL p){LL t=0;for(;b;b>>=1,a=(a<<1)%p)if(b&1)t=(t+a)%p;return t;}
inline LL pow_mod(LL a,LL b,LL p){LL t=1;for(;b;b>>=1,a=mul_mod(a,a,p))if(b&1)t=mul_mod(t,a,p);return t;}
inline LL modinv(const LL &a)  
{  
    LL x1 = 1, x2 = 0, x3 = mod;  
    LL y1 = 0, y2 = 1, y3 = a;  
    while (y3 != 1)  
    {  
        LL k = x3 / y3;  
        x1 -= y1 * k, x2 -= y2 * k, x3 -= y3 * k;  
        swap(x1, y1), swap(x2, y2), swap(x3, y3);  
    }  
    return y2 >= 0 ? y2 : y2 + mod;  
}  
/////////////////////////////////////////////////
void input()
{
    cin>>n>>m>>l;
    mod=n+1;
}
void solve()
{
	LL ans=mul_mod(modinv(pow_mod(2,m,mod)),l,mod);
	cout<<ans<<endl;
}
/////////////////////////////////////////////////
int main()
{
    #ifndef _TEST
    freopen("std.in","r",stdin); freopen("std.out","w",stdout);
    #endif
    input(),solve();
    return 0;
}


PoPopQQQ:

x*2^m==l (mod n+1)
x=(n/2+1)^m*l mod n+1

尼玛这个公式怎么来的???


#define _TEST _TEST
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
/************************************************
Code By willinglive    Blog:http://willinglive.cf
************************************************/
#define rep(i,l,r) for(int i=(l),___t=(r);i<=___t;i++)
#define per(i,r,l) for(int i=(r),___t=(l);i>=___t;i--)
#define MS(arr,x) memset(arr,x,sizeof(arr))
#define LL long long
#define INE(i,u,e) for(int i=head[u];~i;i=e[i].next)
inline const int read()
{int r=0,k=1;char c=getchar();for(;c<'0'||c>'9';c=getchar())if(c=='-')k=-1;
for(;c>='0'&&c<='9';c=getchar())r=r*10+c-'0';return k*r;}
/////////////////////////////////////////////////
LL mod;
LL n,m,l;
/////////////////////////////////////////////////
inline LL mul_mod(LL a,LL b,LL p){LL t=0;for(;b;b>>=1,a=(a<<1)%p)if(b&1)t=(t+a)%p;return t;}
inline LL pow_mod(LL a,LL b,LL p){LL t=1;for(;b;b>>=1,a=mul_mod(a,a,p))if(b&1)t=mul_mod(t,a,p);return t;}
/////////////////////////////////////////////////
void input()
{
    cin>>n>>m>>l;
    mod=n+1;
}
void solve()
{
	cout<<mul_mod(pow_mod(n/2+1,m,mod),l,mod)<<endl;
}
/////////////////////////////////////////////////
int main()
{
    #ifndef _TEST
    freopen("std.in","r",stdin); freopen("std.out","w",stdout);
    #endif
    input(),solve();
    return 0;
}

抱歉!评论已关闭.