UVA10692:Huge Mods
2018-01-19 20:23
309 查看
题面
传送门题意
输入正整数a1,a2,a3..an和模m,求a1^a2^…^an mod mSol
首先有ab≡⎧⎩⎨⎪⎪ab%ϕ(p) gcd(a,p)=1ab gcd(a,p)≠1,b<ϕ(p)ab%ϕ(p)+ϕ(p) gcd(a,p)≠1,b≥ϕ(p) (mod p)递归处理,每次取φ,可以试乘来判断是否会大于φ大于时加上就好了
# include <bits/stdc++.h> # define RG register # define IL inline # define Fill(a, b) memset(a, b, sizeof(a)) using namespace std; typedef long long ll; IL ll Read(){ RG ll x = 0, z = 1; RG char c = getchar(); for(; c < '0' || c > '9'; c = getchar()){ if(c == '#') exit(0); z = c == '-' ? -1 : 1; } for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48); return x * z; } int n, m, a[20]; IL int Phi(RG int x){ RG int cnt = x; for(RG int i = 2; i * i <= x; ++i){ if(x % i) continue; while(!(x % i)) x /= i; cnt -= cnt / i; } if(x > 1) cnt -= cnt / x; return cnt; } IL int Pow(RG ll x, RG ll y, RG ll p){ RG int flg2 = 0, flg1 = 0; RG ll cnt = 1; for(; y; y >>= 1){ if(y & 1) flg1 |= (cnt * x >= p || flg2), cnt = cnt * x % p; flg2 |= (x * x >= p); x = x * x % p; } return cnt + flg1 * p; } IL int Calc(RG int x, RG int p){ if(x == n) return Pow(a[x], 1, p); return Pow(a[x], Calc(x + 1, Phi(p)), p); } int main(RG int argc, RG char* argv[]){ for(RG int Case = 1; ; ++Case){ m = Read(); n = Read(); printf("Case #%d: ", Case); for(RG int i = 1; i <= n; ++i) a[i] = Read(); printf("%d\n", Calc(1, m) % m); } return 0; }
相关文章推荐
- uva 10692 - Huge Mods(数论)
- UVA 10692 - Huge Mods(数论)
- UVA - 10692 Huge Mods (欧拉函数)
- uva 10692 - Huge Mods(数论)
- UVa 10692 - Huge Mods(指数循环节)
- uva 10692——Huge Mods
- [UVA10692]Huge Mods
- UVa 10692 Huge Mods(快速幂)(欧拉定理)
- UVa10692 Huge Mods
- UVa 10692 Huge Mods (指数循环节)
- uva 10692 - Huge Mods(指数循环节)
- UVA 10692 - Huge Mods(数论)
- UVA10692:Huge Mods
- uva 10692 - Huge Mods 指数循环节
- 10692 - Huge Mods(指数循环节)
- 指数循环节 uva 10692
- UVa 10692 - Huge Mod (数论 + 欧拉定理扩展)
- Huge Mods-UVA - 10692
- uva10692-指数循环节
- 数学专项number_theory:UVa 10692