I.数列乘积

版权声明:晓程原创 https://blog.csdn.net/qq_43469554/article/details/88140127

给定一个长度为 n 的数列,首项为 a1 ,通项公式如下
在这里插入图片描述
请你求下面式子的结果:
在这里插入图片描述
其中
∏ 表示累乘,由于结果可能很大,我们输出最后结果对 10^9 + 7取模的结果。

输入格式

输出第一行两个整数 n,a1 ,分别表示数列长度和首项的值。

输出格式

输出表达式对 10^9 + 7 取模的结果。

#include <iostream>
using namespace std;
typedef long long int LL;
const int mod = 1e9 + 7;
LL pow_mod(LL x, LL y, int mod)
{
	LL res = 1, temp = x % mod;
	while (y)
	{
		if (y & 1)
		{
			res = res * temp % mod;
		}
		temp = temp * temp % mod;
		y >>= 1;
	}
	return res;
}
int main()
{
	LL n, a;
	cin >> n >> a;
	if (a == 1)
	{
		cout << pow_mod(2, n, mod) << endl;
	}
	else
	{
		LL p = pow_mod(2, n, mod - 1);
		LL res = pow_mod(a, p, mod) - 1;
		if (res < 0)
		{
			res += mod;
		} 
		res *= pow_mod(a - 1, mod - 2, mod);
		res %= mod;
		cout << res << endl;
	}
	return 0;
} 

猜你喜欢

转载自blog.csdn.net/qq_43469554/article/details/88140127
今日推荐