http://acm.nefu.edu.cn/JudgeOnline/problemshow.php?problem_id=602
description |
对于一个正整数n,求出它的欧拉函数值,其中1<=n<=1000
|
input |
输入数据有多组,每组数据一行,有1个正整数为n。 |
output |
输出n的欧拉函数的值 |
sample_input |
5 100 |
sample_output |
4 40快速幂取模算法模板: |
long long quickmod(long long a, long long b, long long m)
{
long long ans=1;
while(b) // 用一个循环 从最右向左遍历b的所有二进制位
{
if(b&1) //位运算 按位与操作 相当于b%2==1 也就是判段bi是否为1
{
ans=(ans*a)%m;
//乘到结果上 这里的a是 a^(2^i)%m
b--;
//把该位变0(这句可有可无)
}
b>>1;
//相当于计算接下来左边的二进制位
a=a*a%m;
//a随着b的移位 不断放大 但是需要对m求余防止数大超范围
}
return ans;
}
本题代码:
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
long long a,b,c;
while(~scanf("%lld%lld%lld",&a,&b,&c))
{
long long ans=1;
while(b)
{
if(b&1)
{
ans=(ans*a)%c;
}
b=b>>1;
a=a*a%c;
//printf("%lld**\n",b);
}
printf("%lld\n",ans);
}
return 0;
}