uva 10692 Huge Mods (欧拉函数,指数循环节)

题目链接

题意:求一个楼梯数%m的大小。

思路:指数循环节。

需要注意的是,模数只有最外层是m,每往里一层,模数都变成m=phi(m)

所以可以写个dfs或者先预处理出每一层m存一下。

记得考虑n=1的特殊情况。

  1/* ***********************************************
  2Author :111qqz
  3Created Time :Wed 26 Oct 2016 07:07:27 PM CST
  4File Name :code/uva/10692.cpp
  5************************************************ */
  6#include <cstdio>
  7#include <cstring>
  8#include <iostream>
  9#include <algorithm>
 10#include <vector>
 11#include <queue>
 12#include <set>
 13#include <map>
 14#include <string>
 15#include <cmath>
 16#include <cstdlib>
 17#include <ctime>
 18#define fst first
 19#define sec second
 20#define lson l,m,rt<<1
 21#define rson m+1,r,rt<<1|1
 22#define ms(a,x) memset(a,x,sizeof(a))
 23typedef long long LL;
 24#define pi pair < int ,int >
 25#define MP make_pair
 26using namespace std;
 27const double eps = 1E-8;
 28const int dx4[4]={1,0,0,-1};
 29const int dy4[4]={0,-1,1,0};
 30const int inf = 0x3f3f3f3f;
 31char st[20];
 32LL n,m;
 33LL a[15];
 34LL ksm( LL a,LL b,LL k)
 35{
 36    LL res = 1;
 37    while (b>0)
 38    {
 39	if (b&1) res = (res * a )% k;
 40	b = b >> 1;
 41	a = ( a * a) % k;
 42    }
 43    return res;
 44}
 45LL euler( LL x)
 46{
 47    LL ret = 1 ;
 48    for ( LL i = 2 ; i*i <= x ; i++)
 49    {
 50	if (x%i==0)
 51	{
 52	    x/=i;
 53	    ret*=(i-1);
 54	    while (x%i==0)
 55	    {
 56		x/=i;
 57		ret*=i;
 58	    }
 59	}
 60    }
 61    if (x>1) ret*=(x-1);
 62    return ret;
 63}
 64LL phi[20];
 65int main()
 66{
 67	#ifndef  ONLINE_JUDGE 
 68	freopen("code/in.txt","r",stdin);
 69  #endif
 70	int cas = 0 ;
 71	while (~scanf("%s",st))
 72	{
 73	    ms(a,0);
 74	    ms(phi,0);
 75	  //  cout<<"st:"<<st<<endl;
 76	    if (st[0]=='#') break;
 77	    int len = strlen(st);
 78	    m = 0 ;
 79	    for ( int i = 0 ; i < len ; i++)
 80	    {
 81		LL val = st[i]-'0';
 82		m = m * 10 + val;
 83	    }
 84	  //  cout<<"m:"<<m<<endl;
 85	    scanf("%lld",&n);
 86	    phi[0] = m;
 87	    for ( int i = 1; i <= n ; i++) scanf("%lld",a+i);
 88	    LL fi = euler(m);
 89	    for ( int i = 1 ; i <= n-1 ;i++)
 90	    {
 91		phi[i] = fi;
 92		fi = euler(fi);
 93	    }
 94	    for ( int i = n-1 ; i >=1 ; i--)
 95	    {
 96		a[i] = ksm(a[i],(a[i+1]%phi[i]+phi[i]),phi[i-1]);
 97	    }
 98	    printf("Case #%d: %lld\n",++cas,a[1]%m);  //考虑n为1的情况
 99	    ms(st,0);
100	}
101  #ifndef ONLINE_JUDGE  
102  fclose(stdin);
103  #endif
104    return 0;
105}