hdu 4704 Sum(费马小定理)

来源:互联网 发布:windows设置ntp客户端 编辑:程序博客网 时间:2024/05/29 03:19

数论,费马小定理 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);#endifwhile (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;}


0 0
原创粉丝点击