HDU 4704 Sum (欧拉降幂+二项式公式+快速幂)

题解

给你一个n让你求有多少个数列相加可以得到这个n,要求所有数为正整数,那么根据隔板法列出排列组合公式,随后进行二项式转化,那么我们求的就是2的n-1次方,但是由于n过于大,我们需要使用欧拉降幂,因为2和mod互质,由欧拉降幂公式转换后,我们只需要对这个大数n模上一个mod-1,随后对其做快速幂乘就可以了。

Code

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e5+10;
typedef pair<int,int> PII;
const ll mod=1e9+7;
string s;

ll fast_pow (ll x,ll n) {
	ll ans=1%mod;
	while (n) {
		if (n&1) ans=(ans*x)%(mod);
		x=(x*x)%(mod);
		n>>=1;
	}
	return ans%(mod);
}

int main () {
    ios::sync_with_stdio (false);
	while (cin>>s) {
		ll len=s.size ();
		ll sum=0;
		for (int i=0;i<len;i++) {
			sum=sum*10+s[i]-48;
			sum%=mod-1;
		}
		sum-=1;
		ll ans=fast_pow (2,sum);
		printf ("%lld
",ans);
	}
	return 0;
}
原文地址:https://www.cnblogs.com/hhlya/p/14225158.html