bzoj 1965 [Ahoi2005]SHUFFLE 洗牌 数学

题面

题目传送门

解法

可以发现,位置为\(x\)的经过一次操作后就会变成\(2x\ mod\ (n+1)\)

然后就变成\(x×2^m\equiv k(mod\ n+1)\)

然后求一个逆元即可

代码

#include <bits/stdc++.h>
#define int long long
using namespace std;
int n, m, k, p;
int Pow(int x, int y) {
    int ret = 1;
    while (y) {
        if (y & 1) ret = ret * x % p;
        y >>= 1, x = x * x % p;
    }
    return ret;
}
void calc(int a, int b, int &x, int &y) {
    if (b == 0) {x = 1, y = 0; return;}
    int r = a % b, q = a / b;
    calc(b, r, y, x);
    y -= x * q;
}
main() {
    cin >> n >> m >> k; p = n + 1;
    int a = Pow(2, m), tx, ty;
    calc(a, p, tx, ty);
    tx = (tx % p + p) % p;
    cout << k * tx % p << "\n";
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/copperoxide/p/9476729.html