intex_gcd(int a, int b, int& x, int& y){
if (b == 0) {
x = 1;
y = 0;
return a;
}
int d = ex_gcd(b, a % b, x, y);
int temp = x;
x = y;
y = temp - a / b * y;
return d;
}
intex_gcd(int a, int b, int& x, int& y){
if (b == 0) {
x = 1;
y = 0;
return a;
}
int d = ex_gcd(b, a % b, x, y);
int temp = x;
x = y;
y = temp - a / b * y;
return d;
}
boolliEu(int a, int b, int c, int& x, int& y){
int d = ex_gcd(a, b, x, y);
if (c % d != 0) return0;
int k = c / d;
x *= k;
y *= k;
return1;
}
乘法逆元
指模意义下的乘法逆元。
定义
如果有 x 满足线性同余方程 ax≡1(modb) ,则 x 被称为 amodb 的逆元,记作 x=a−1 。
#include<bits/stdc++.h>usingnamespace std;
constint N = 1e6 + 10;
int a, n;
intexgcd(int a, int n, int& x, int& k){
if (n == 0) {
x = 1, k = 0;
return a;
}
int t = exgcd(n, a % n, x, k);
int xx = x;
x = k, k = xx - a / n * k;
return t;
}
intmain(){
scanf("%d%d", &a, &n);
int x, k;
int t = exgcd(a, n, x, k);
t = n / t;
printf("%d\n", (x % t + t) % t);
return0;
}
#include<bits/stdc++.h>usingnamespace std;
constint N = 5e6 + 10;
longlong n, p, K;
longlong a[N], s[N], sv[N];
longlongread(){
longlong x=0,flag=1;char c;
while ((c=getchar())<'0' || c>'9') if(c=='-') flag=-1;
while (c>='0' && c<='9') x=(x<<3)+(x<<1)+(c^48),c=getchar();
return x*flag;
}
intexgcd(int a, int n, int& x, int& k){
if (n == 0) {
x = 1, k = 0;
return a;
}
int t = exgcd(n, a % n, x, k);
int xx = x;
x = k, k = xx - a / n * k;
return t;
}
intmain(){
s[0] = 1;
n = read(); p = read(); K = read();
for (int i = 1; i <= n; i++) {
a[i] = read();
s[i] = s[i - 1] * (a[i] % p) % p;
}
int x, k;
int b = exgcd(s[n], p, x, k);
b = p / b;
sv[n] = (x % b + b) % b;
for (int i = n; i >= 1; i--) {
sv[i - 1] = sv[i] * a[i] % p;
}
longlong ans = 0, t = 1, inv;
for (int i = 1; i <= n; i++) {
t = t * K % p;
inv = sv[i] * s[i - 1] % p;
ans = (ans + inv * t % p) % p;
}
printf("%lld\n", ans);
return0;
}
#include<bits/stdc++.h>usingnamespace std;
constint N = 1e6 + 10;
longlong n, m, L;
longlongexgcd(longlong a, longlong n, longlong& x, longlong& k){
if (n == 0) {
x = 1, k = 0;
return a;
}
longlong t = exgcd(n, a % n, x, k);
longlong xx = x;
x = k, k = xx - a / n * k;
return t;
}
longlongqmul(longlong a, longlong b, longlong mod){
longlong ans = 0;
while (b) {
if (b & 1) {
ans = (ans + a) % mod;
}
a = (a + a) % mod;
b >>= 1;
}
return ans;
}
longlongqpow(longlong a, longlong b, longlong mod){
longlong ans = 1, base = a;
while (b) {
if (b & 1) {
ans = qmul(ans, base, mod);
}
base = qmul(base, base, mod);
b >>= 1;
}
return ans;
}
intmain(){
scanf("%lld%lld%lld", &n, &m, &L);
longlong x, k;
longlong t = exgcd(2, n + 1, x, k);
t = (n + 1) / t;
longlong inv2 = (x % t + t) % t;
printf("%lld\n", qmul(L, qpow(inv2, m, n + 1), n + 1));
return0;
}