1. 程式人生 > >bzoj 1965 [Ahoi2005]SHUFFLE 洗牌 數學

bzoj 1965 [Ahoi2005]SHUFFLE 洗牌 數學

傳送門 return pow mod shuffle 發現 zoj ace cal

題面

題目傳送門

解法

可以發現,位置為\(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;
}

bzoj 1965 [Ahoi2005]SHUFFLE 洗牌 數學