UVA10692+636

来源:互联网 发布:知天下资源吗邀请码 编辑:程序博客网 时间:2024/06/09 22:59

The operator for exponentiation is differentfrom the addition, subtraction, multiplicationor division operators in the sensethat the default associativity for exponentiationgoes right to left instead of left toright. So unless we mess it up by placingparenthesis, 232should mean 2(32) = 29 = 512 not (23)2 = 82 = 64 . This leads to the obvious fact thatif we take the levels of exponents higher (i.e., 2ˆ3ˆ4ˆ5ˆ3), the numbers can become quite big. But let’snot make life miserable. We being the good guys would force the ultimate value to be no more than10000.

Given a1, a2, a3, . . . , aN and m(= 10000) you only need to compute a1ˆa2ˆa3ˆ. . .ˆaN mod m

Input

There can be multiple (not more than 100) test cases. Each test case will be presented in a single line.The first line of each test case would contain the value for M (2 ≤ M ≤ 10000). The next number ofthat line would be N (1 ≤ N ≤ 10). Then N numbers — the values for a1, a2, a3, . . . , aN would follow.You can safely assume that 1 ≤ ai ≤ 1000. The end of input is marked by a line containing a singlehash (‘#’) mark.

Output

For each of the test cases, print the test case number followed by the value of a1ˆa2ˆa3ˆ. . .ˆaN mod mon one line. The sample output shows the exact format for printing the test case number.

Sample Input

10 4 2 3 4 5

100 2 5 2

53 3 2 3 2

#

Sample Output

Case #1: 2

Case #2: 25

Case #3: 35

题意: 

与此类似

思路:

知道a^x=a^(x%phi(c)+phi(c)) (mod c)递归即可

#include<iostream>//a^x=a^(x%phi(c)+phi(c)) (mod c)#include<stdio.h>#include<string.h>using namespace std;#define ll long long#define N 10010ll map[N];ll n;ll ppow(ll a,ll b,ll mod){    ll ans=1;    ll base=a;    while(b!=0)    {        if(b&1!=0)            ans=ans*base%mod;        base=base*base%mod;        b>>=1;    }    return ans;}ll phi[N];void Oula(){    memset(phi,0,sizeof(phi));    phi[1]=1;    for(int i=2;i<N;i++)        if(!phi[i])       for(int j=i;j<N;j+=i)       {           if(!phi[j])           phi[j]=j;           phi[j]=phi[j]/i*(i-1);       }}int solve(int now,int mod){    if(now==n)    {        return map[now]%mod;    }    int t=solve(now+1,phi[mod]);    int ans=ppow(map[now],t+phi[mod],mod);    return ans;}int main(){    Oula();    string s;    int cas=1;    int mod;    while(cin>>s)    {        mod=0;        if(s[0]=='#')            break;        for(int i=0;i<s.size();i++)            mod=mod*10+s[i]-'0';        cin>>n;        for(int i=1;i<=n;i++)            cin>>map[i];        cout<<"Case #"<<cas++<<": ";        cout<<solve(1,mod)<<endl;    }    return 0;}

A few days earlier a certain mathematician had been fired from his job so he has made up his mindto take revenge on his former employers and has changed all the numbers in their databases to theircorresponding forms in different numerical systems using different bases. At the beginning it seemed toeveryone to be just a stupid joke and hopefully they would soon find the correct data hidden somewhere.They were wrong, because even the backup database copies have been changed. The only hint, theywere given was that all the data had been transformed to systems with such a base that it is the smallestbase in which input numbers are squares.

Your task is to find these bases. You need to hurry up, because the whole firm’s activity dependson your database fix. You may however assume that:

• for each number, there exists a sought base and it is less than 100

• all the digits in input numbers are characters 0, 1, 2, 3, 4, 5, 6, 7, 8, 9

• each number written in decimal system is smaller that 1000000000

InputData set consists of lines containing single numbers. Occurrence of ‘0’ means the end of data set (0 isnot treated as valid data).

Output

For each number you should find a smallest base of a numerical system in which this number is a squareof some other number. Each number should be outputted in separate line.

Sample Input

61

1100

509

510

1013

0

Sample Output

8

3

12

16

6

题意:给你一串数字,问你他最小的基底,是的这个数是一个平方数

1013例     ① 1   ②(1*6+0)=6   ③ 6*6+1=37  ④ 37*6+3=225

509     ① 5   ②   5*12+0=60   ③    60*12+9=729 

思路:模拟  枚举  判断是不是平方数

#include<iostream>#include<stdio.h>#include<string.h>#include<cmath>using namespace std;#define ll long long#define INF 1e-9#define N 10int max(ll n){    int ans=0;    while(n)    {        if(ans<n%10)        {            ans=n%10;        }        n/=10;    }    return ans;}ll judge1(ll n,int base){    ll ans=0;    int map[N];    int num=0;    while(n)    {        map[num]=n%10;        //cout<<map[num]<<" ";        num++;        n/=10;    }   // cout<<endl;    while(num)    {        num--;        ans*=base;        ans+=map[num];       //cout<<"num="<<num<<" "<<"ans="<<ans<<endl;    }    return ans;}int judge2(ll p){     double temp1=sqrt(p);    int temp2=sqrt(p);    if(fabs(temp1-temp2)<INF)        return 1;    else return 0;}int main(){    ll n;    while (cin>>n)    {        if(n==0)        break;        //cout<<max(n)<<endl;        for(int i=max(n)+1;i<100;i++)        {            if(judge2(judge1(n,i)))            {                cout<<i<<endl;                break;            }        }    }    return 0;}



原创粉丝点击