递归用的很巧妙,虽然不是我想出来的
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<vector>
#include<queue>
#define ls 2*rt
#define rs 2*rt+1
#define lson ls,L,mid
#define rson rs,mid+1,R
#define ll long long
using namespace std;
typedef pair<int,int> pii;
const ll inf = 0x3f3f3f3f;
/*void dis(int a[], int n){printf("总数为%d个\n",n); for(int i = 0; i < n; i++) cout<<a[i]<<", ";cout<<endl<<"------------------"<<endl;
}*/
const int mx = 1e3+100;
char s[mx];
int a[mx],mod,n;ll eular(ll n)
{ll ret=1,i;for(i=2;i*i<=n;i++){if(n%i==0){n/=i,ret*=i-1;while(n%i==0) n/=i,ret*=i;}}if(n>1) ret*=n-1;return ret;
}
ll power(ll a, ll b, ll m){if(b == 0)return 1;ll ans= 1;a %= m;while(b){if(b%2)ans = ans*a%m;b/=2;a = a*a%m;}return ans;
}
ll slove(int i, int mod){if(i == n-1)return a[i]%mod;int eu = eular(mod);int nu = slove(i+1,eu) + eu;return power(a[i],nu,mod);
}int main(){int ca = 0;while(scanf("%s",s) && s[0] != '#'){mod = 0;int len = strlen(s);for(int i = 0; i < len; i++)mod = mod*10+s[i]-'0';scanf("%d",&n);for(int i = 0; i < n; i++){scanf("%d",a+i); }printf("Case #%d: %lld\n",++ca,slove(0,mod));}return 0;
}