HDU - 4704 - Sum 【费马小定理 + 快速幂 】 题解

1.题目

在这里插入图片描述
Input
2
Output
2

Hint

  1. For N = 2, S(1) = S(2) = 1.

  2. The input file consists of multiple test cases.

Sample Input
2
Sample Output
2

2.代码

#include<iostream>
#include <stdio.h>
using namespace std;
char N[100005];
const int mod=1e9+7;
//费马小定理
long long PhiMa(char *N,int mod)
{
    long long ans=0;
    for(int i=0;N[i]!='\0';i++)
        ans=(ans*10+N[i]-'0')%mod;
    return ans;
}
//快速幂取余运算
long long FastPow(long long a,long long b)
{
   long long ans=1;
   while(b)
   {
       if(b&1)
         ans=ans*a%mod;
       a=a*a%mod;
       b>>=1;
   }
   return ans;
}
int main()
{
    while(scanf("%s",N)!=EOF)
    {
        long long n=PhiMa(N,mod-1)-1;
        printf("%d\n",FastPow(2,n));
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_45629285/article/details/106810797