您的位置:首页 > 其它

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  phi 指数循环节 数学