题目链接:点击打开链接
比赛的时候思路跑偏了,总是纠结于找第一行的每个元素对于答案的贡献,找了一个多小时然后放弃了。
暴力打表然后找规律。
因为存在除法取模的问题,所以要用到逆元。
代码如下:
#include <bits/stdc++.h>
using namespace std;
typedef long long int LL;
const int MAX_N = 1000;
const int MOD = 1e9 + 7;
// 拓展欧几里得算法
void extgcd(LL a, LL b, LL& x, LL& y)
{
if (!b)
{
x = 1;
y = 0;
}
else
{
extgcd(b, a % b, y, x);
y -= (a / b) * x;
}
}
LL mod_pow(LL a, LL n, LL mod)
{
LL res = 1;
while (n)
{
if (n & 1)
res = res * a % mod;
a = a * a % mod;
n >>= 1;
}
return res;
}
int main()
{
//freopen("test.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
cin.sync_with_stdio(false);
int T;
LL n, m;
cin >> T;
while (T--)
{
cin >> n >> m;
if (m == 1)
{
cout << 1 << endl;
continue;
}
LL x, y;
extgcd(3, MOD, x, y);
x = (x % MOD + MOD) % MOD; // x为3模1e9+7的逆元
int ans;
if (n % 2)
ans = (mod_pow(mod_pow(2, n, MOD) - 1, m - 1, MOD) * 2 % MOD + 1) * x % MOD;
else
ans = (mod_pow(mod_pow(2, n, MOD) - 1, m - 1, MOD) * 2 % MOD) * x % MOD;
cout << ans << endl;
}
return 0;
}