相爱 | ||||||
| ||||||
Description | ||||||
静竹在斐波那契的帮助下,渐渐的和数学相爱了。和数学在一起最有意思的就是它能够出一些特别有意思并且巧妙的题目让静竹来思考。这次也不例外,给静竹两个数a,b,又给出了加,减,乘,除,乘方五种操作六种算式(a + b, a – b, a * b, a / b, a ^ b, b ^ a)让静竹从中挑一种操作使得a,b处理后数值变得最大。静竹思考了一番发现,这个结果可能会特别大怎么办呢?为了解决这个问题,数学又给出了一个数m,让最终结果对其求余就可以让结果变小了。 | ||||||
Input | ||||||
每行输入三个整数a, b, m(0 <=a, b <=100000000, 0< m <1000) | ||||||
Output | ||||||
对于每行的输入,输出一个整数代表结果最终的结果 | ||||||
Sample Input | ||||||
2 3 5 3 2 5 | ||||||
Sample Output | ||||||
4 4 | ||||||
Author | ||||||
万祥 |
解题思路:当两个数都比较小时,可以每种都判断一下(注意b等于0不能除),当有一个比较大时,那么必然是大的那个作为指数的幂比较大(这题数据水了,对于0和1应该 是需要特判的,但没特判也对了)
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <vector>
#include <bitset>
#include <set>
#include <stack>
#include <map>
#include <climits>
#include <functional>
using namespace std;
#define LL long long
const LL INF=0x3f3f3f3f3f3f3f3f;
LL a,b,mod;
LL qmod(LL a,LL b)
{
LL ans=1;
a%=mod;
while(b>0)
{
if(b%2==1)ans=(ans*a)%mod;
b/=2;
a=(a*a)%mod;
}
return ans;
}
LL qmod2(LL a,LL b)
{
LL ans=1;
while(b>0)
{
if(b%2==1)ans=(ans*a);
b/=2;
a=(a*a);
}
return ans;
}
int main()
{
while(~scanf("%lld %lld %lld",&a,&b,&mod))
{
LL ma=0;
if(a==0||b==0) {printf("%d\n",(a+b)%mod);continue;}
if(a==1||b==1) {printf("%d\n",(a+b)%mod);continue;}
if(a<=10&&b<=10)
{
ma=max(a+b,a-b);
ma=max(ma,a*b);
if(b!=0)
ma=max(ma,a/b);
ma=max(max(qmod2(a,b),qmod2(b,a)),ma);
printf("%lld\n",ma%mod);
}
else
{
if(a>b)swap(a,b);
printf("%lld\n",qmod(a,b));
}
}
return 0;
}