zoukankan      html  css  js  c++  java
  • [luogu2054 AHOI2005] 洗牌 (数论)

    传送门

    Solution

    我们考虑每一步牌的变化:

    • 前半部分的牌位置*2
    • 后半部分的牌位置*2-n-1

    那么我们可以看做是(x imes 2^mequiv 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;
    }
    
  • 相关阅读:
    2019春招面试题总结-03
    2019春招面试题总结-02
    2019春招面试题总结-01
    Node.js 全局对象
    Node.js 路由
    Node.js 函数
    Node.js 模块系统
    Node.js Stream(流)
    Node.js Buffer(缓冲区)
    Node.js EventEmitter
  • 原文地址:https://www.cnblogs.com/Menteur-Hxy/p/9740840.html
Copyright © 2011-2022 走看看