1. 程式人生 > >hdu 4704 Sum(費馬小定理)

hdu 4704 Sum(費馬小定理)

數論,費馬小定理 a^(p-1) % p == 1,長見識了

#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
typedef __int64 ll;
const int MAXN = 100005;
char s[MAXN];
const ll mod = 1000000007;
ll n;
ll power(ll x, ll t)
{
	ll res = 1; ll tp = x;
	while (t)
	{
		if (t & 1) res = res*tp%mod;
		tp = tp*tp%mod;
		t >>= 1;
	}
	return res;
}
int main()
{
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
#endif
	while (scanf("%s", s) != EOF)
	{
		n = 0;
		int k = strlen(s);
		for (int i = 0; i < k; ++i)
		{
			n = (n*10 + s[i] - '0')%(mod-1);
		}
		n = (n-1+mod-1)%(mod-1);
		printf("%I64d\n", power(2, n));
	} 
    return 0;
}