hdu 4704 Sum(费马小定理)

本文介绍了一个基于费马小定理的应用实例,通过C++代码展示了如何使用快速幂算法计算特定数学问题。重点在于理解费马小定理a^(p-1)%p==1,并利用此原理进行高效计算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值