10692 - Huge Mods(指数循环节)
2017-04-26 22:27
399 查看
题目链接
10692 - Huge Mods分析
其实这个问题很简单,有以下定理可用若(a,n)=1,ax≡ax mod ϕ(m)(mod m)
若(a,n)≠1,ax≡ax mod ϕ(m)+ϕ(m)(mod ϕ(m))
因此我们可以递归的求解这个问题.
f(aa2a3…1)≡af(a2a3…)modϕ(m)+ϕ(m)1(mod m)
注意一定要加上ϕ(m)因为当整除的时候会出现问题.
#include <cstdio> #include <iostream> #include <vector> #include <queue> #include <algorithm> #include<cmath> #include <cstring> #include <map> #include <set> #include <iomanip> #include <bitset> #define pb push_back #define mp make_pair #define fi first #define se second #define INF 0x3f3f3f3f #define INF64 0x3f3f3f3f3f3f3f3f using namespace std; const int mod = 1e8+7; const int MAX_P = 2e4+10; const int maxn =1e5+10; const int MAX_V = 5e5+10; const int maxv = 1e6+10; typedef long long LL; typedef long double DB; typedef pair<int,int> Pair; int n; int a[maxn]; int phi[maxn],prime[maxn],cnt; void phi_table() { memset(prime,0,sizeof(prime)); cnt =0; phi[1] = 1; for(int i=2 ; i<maxn ; ++i) { if(!prime[i]){ prime[cnt++] = i; phi[i] = i-1; } for(int j =0 ; j< cnt && i*prime[j] < maxn ; ++j){ prime[i*prime[j]] = 1; if(i % prime[j])phi[i*prime[j]] = phi[i]*(prime[j]-1); else{ phi[i*prime[j]] = phi[i]*prime[j]; break; } } } } LL power_mod(LL a, LL b, LL mod) { LL ans = 1; while(b > 0) { if(b & 1) ans = ans * a % mod; a = a * a % mod; b >>= 1; } return ans + mod; } LL f(int idx,LL mod){ if(idx == n-1) return a[idx] < mod ? a[idx] : a[idx]% mod + mod; return power_mod(a[idx],f(idx+1,phi[mod]),mod); } int main() { // ios::sync_with_stdio(false); // cin.tie(nuLLptr); // cout.precision(10); // cout << fixed; #ifdef LOCAL_DEFINE freopen("in.txt", "r", stdin); #endif phi_table(); // for(int i=1 ; i< 100 ; ++i) // std::cout << phi[i] << '\n'; int m; int kase =0; while (cin>>m && m != (int)'#') { //std::cout << m << '\n'; cin>>n; for(int i=0 ; i<n ; ++i)scanf("%d",&a[i] ); printf("Case #%d: %lld\n",++kase,f(0,m)%m ); } #ifdef LOCAL_DEFINE cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n"; #endif return 0; }
相关文章推荐
- UVa 10692 - Huge Mods(指数循环节)
- uva 10692 - Huge Mods(指数循环节)
- uva 10692 - Huge Mods 指数循环节
- UVa 10692 Huge Mods (指数循环节)
- uva10692 - Huge Mods poj2164: Remainder Calculator 指数循环节
- UVa10692,Huge Mod,数论,欧拉定理,指数循环节
- 指数循环节 uva 10692
- uva10692-指数循环节
- hdu 3307 简单的指数循环节
- UVa10692 Huge Mods
- HDU 5728 PowMod (欧拉函数+指数循环节)
- UVa 10692 Huge Mods(快速幂)(欧拉定理)
- hdu 5895 Mathematician QSC(快速幂+指数循环节)
- hdu 5728 (公式推导+指数循环节)
- bzoj-3884(指数循环节)
- hdu 3221 (指数循环节)
- hdu 2837 Calculation【欧拉函数,快速幂求指数循环节】
- 指数循环节的证明
- 【关于 A^x = A^(x % Phi(C) + Phi(C)) (mod C) 的若干证明】【指数循环节】
- 【转】【关于 A^x = A^(x % Phi(C) + Phi(C)) (mod C) 的若干证明】【指数循环节】