当前位置: 代码迷 >> 综合 >> UVa - 10692 - Huge Mods-(幂的模)
  详细解决方案

UVa - 10692 - Huge Mods-(幂的模)

热度:96   发布时间:2024-01-12 15:42:13.0


代码:

#include<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<queue>
#include<cstring>
#include<map>
using namespace std;
typedef long long ll;
#define M 1005char str[25];
int n,m,ans;
int a[25];int pow_mod(int n,int k,int m) //快速幂
{int res=1;while(k>0){if(k&1)res=res*n%m;n=n*n%m;k>>=1;}return res;
}int euler_phi(int n)
{int m=(int)sqrt(n+0.5);int ans=n;for(int i=2;i<=m;i++){if(n%i==0){ans=ans/i*(i-1);while(n%i==0)n/=i;}}if(n>1)ans=ans/n*(n-1);return ans;
}int solve(int i,int mod)
{if(i==n-1){return a[i]%mod;}int phi=euler_phi(mod);int k=solve(i+1,phi)+phi;return pow_mod(a[i],k,mod);
}int main()
{int i,cas=0;while(scanf("%s",str)==1&&strcmp(str,"#")!=0){sscanf(str,"%d",&m);scanf("%d",&n);for(i=0;i<n;i++)scanf("%d",&a[i]);ans=solve(0,m);printf("Case #%d: %d\n",++cas,ans);}return 0;
}