CG的通关秘籍

链接:https://ac.nowcoder.com/acm/contest/10845/C
来源:牛客网

CG最喜欢玩的就是拼图游戏,但是他已经通关了所有拼图游戏,感觉拼图游戏已经没有了任何的乐趣。所以今天他选择玩填数游戏。

CG每次填一个的数到当前位置,如果这个位置填的数比上一次填的数要大,形成顺序,他的兴奋度会增加1点,如果这个数比上一次填的数要小,形成逆序,他的兴奋度会增加2点,如果两个数相等,那么什么都不会发生。(如果是第一次填数,同样不会发生任何事情)

CG认为如果已知他n次填的数,计算出当他填了n个数之后的兴奋度太简单了,所以想要你帮他计算一下他所有填数方案的兴奋度之和。

由于这个结果过大,将这个结果

输入描述:
第一行输入一个正整数t(t<=10^6)t(t<=10
6
)表示输入组数。

接下来t行,每行有一个n,m(2<=n<=106,1<=m<=106)n,m(2<=n<=10
6
,1<=m<=10
6
),n表示每次填数次数,m表示填数范围。
输出描述:
对于每一组数组,输出所有填数方案的兴奋度之和。
示例1
输入
复制
1
2 2
输出
复制
3
说明
输入数据量较大,建议使用较快的输入输出方法

在这里插入图片描述

#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <queue>
#include <map>
#include <stack>
#include <map>
#include <unordered_map>
#include <vector>
#include <cmath>
#include <ext/rope>
#include <set>
using namespace std;
using namespace __gnu_cxx;

#define pair(a, b) make_pair(a, b)
#define memset(a, b) memset(a, b, sizeof a)
#define max(a, b) ((a) < (b) ? (b) : (a))
#define min(a, b) ((a) < (b) ? (a) : (b))
//#define x first
//#define y second

int dx[4] = {
    
    0, 1, 0, -1};
int dy[4] = {
    
    1, 0, -1, 0}; 

//typedef __int128 INT;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;

const int N = 1e5 + 10;
const int M = 2e5 + 10;
const int Mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int P = 13331;

LL n, m, k;

LL quick_mi(int a, int b)
{
    
    
    LL res = 1;
    LL mul = a;
    while (b)
    {
    
    
        if (b & 1) res = (res * mul) % Mod;
        mul = (mul * mul) % Mod;
        b >>= 1;
    }
    return res;
}

int main()
{
    
    
	//freopen("C:\\Users\\86187\\Desktop\\stdin.txt", "r", stdin);
	
	int T;
	cin >> T;
	while (T --)
	{
    
    
		scanf("%d%d", &n, &m);
		
		LL mul = quick_mi(m, n - 2);
		
		
		LL res = (3 * (m - 1) * m / 2) % Mod;
		
		res = (((res * (n - 1)) % Mod) * mul) % Mod;
		
		printf("%lld\n", res);
	}
	
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_45772483/article/details/112689283