CG clearance cheats

Link: https://ac.nowcoder.com/acm/contest/10845/C
Source: Niuke.com

CG likes to play jigsaw puzzles the most, but he has cleared all the jigsaw puzzles and feels that jigsaw puzzles have no fun anymore. So today he chose to play the puzzle game.

CG fills in one count at a time to the current position. If the number filled in this position is larger than the number filled last time, forming a sequence, his excitement will increase by 1 point. If this number is smaller than the number filled last time, In the reverse order, his excitement will increase by 2 points. If the two numbers are equal, nothing will happen. (If it is the first time to fill in, nothing will happen either)

CG thinks that if he knows the number he filled in n times, it is too simple to calculate the excitement after he filled in n numbers, so I want you to help him calculate the total excitement of all his filling plans.

Since this result is too large, the result

Input description:
Input a positive integer t(t<=10^6)t(t<=10
6
) in the first line to indicate the number of input groups.

Next t rows, each row has a n,m(2<=n<=10 6,1<=m<=10 6)n,m(2<=n<=10
6
,1<=m<= 10
6
), n represents the number of fillings each time, and m represents the range of fillings.
Output description:
For each group of arrays, output the sum of excitement of all filling schemes.
Example 1
Input
Copy
1
2 2
Output
Copy
3
Description The
amount of input data is large, it is recommended to use a faster input and output method

Insert picture description here

#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;
}

Guess you like

Origin blog.csdn.net/qq_45772483/article/details/112689283