NiuniuとNiuCokeのギャンブル契約

リンク:https
://ac.nowcoder.com/acm/contest/11334/A出典:Niuke

説明タイトル
ウシコーラの発明n面ダイ(それぞれ{1}から{1} NNを指し、各平面のスロー確率は\ FRAC {1} {n}
n
1 )ビーフ、ビーフで遊ぶため彼はヨーロッパの皇帝なので、ニウニウの性格をテストしたかったのです。彼はニウニウにm {} mのダイスを転がすように言いました。ニウニウがすべての顔を{} nnで投げると、ニウニウが勝ったとしても、ニウニウは自分を信じます。ピン、ニウ・コークと一緒にスパイシーなバーのパックに賭けて、{} nnヌードルのすべてのポイントを確実にキャストできると言ったが、ニウ・ニウは賭けに負けるのを少し恐れている。事前に負ける確率を計算するのを手伝ってほしい?入力説明:そこに入力例の複数のグループであり、最初の行はサンプルグループのT(T \当量1×10 ^ 6)T(t≤1×10の数である6 次T線それぞれは、整数n及びmを有します、ダイスの顔の数と雄牛の投げの数を示します






(n、m <= 1×10 ^ 9)(n、m <= 1×
109

出力の説明:
出力t行、各行はスコアp / q mod 1e9 +7の形式で出力されます

例1
入力
コピー
1
2 1
出力
コピー
500000004
備考:
データが大きい場合、より速く入力と出力を使用することをお勧めします

ここに写真の説明を挿入

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int mod = 1e9 + 7;

typedef long long LL;

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

int main(){
    
    
	int T;
	scanf("%d", &T);
	
	while(T --){
    
    
		int n, m;
		scanf("%d%d", &n, &m);
	//	cout << qmi(2, 5, mod) << endl;
		LL P1 = qmi(n, m, mod) - 1;
		LL temp = P1 + 1;
	//	cout << temp << endl;
		LL P2 = qmi(temp, mod - 2, mod);
	//	cout << P2 << endl;
		LL ans = (P1 * P2) % mod;
		
		printf("%lld\n", ans);
	}
	
	return 0;
}

おすすめ

転載: blog.csdn.net/qq_45772483/article/details/112554538