結果

問題 No.823 Many Shifts Easy
ユーザー okok
提出日時 2019-04-26 23:57:31
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,433 bytes
コンパイル時間 720 ms
コンパイル使用メモリ 73,268 KB
実行使用メモリ 6,940 KB
最終ジャッジ日時 2024-05-04 02:20:14
合計ジャッジ時間 1,248 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 5 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 WA -
testcase_06 AC 4 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 3 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<string>
#include<iomanip>
#include<cmath>
#include<vector>
#include<algorithm>

using namespace std;

#define int long long
#define rep(i,n) for(int i = 0; i < (n); i++)
#define INF ((long long)1e18)
#define MOD ((int)1e9+7)
#define endl "\n"

#define yn(f) ((f)?"Yes":"No")
#define YN(f) ((f)?"YES":"NO")

#define MAX 110000

int fac[MAX], mmi[MAX];

void exgcd(int a, int b, int &x, int &y){
	if(b == 0){
		x = 1;
		y = 0;
		return ;
	}
	exgcd(b,a%b,y,x);
	y -= a/b * x;
}

void init(int n){
	fac[0] = 1;
	for(int i = 1; i <= n; i++){
		fac[i] = (fac[i-1]*i)%MOD;
	}
	int x, y;  
	exgcd(fac[n],MOD,x,y);
	mmi[n] = x%MOD; 
	
	for(int i = n-1; i >= 0; i--){
		mmi[i] = mmi[i+1]*(i+1)%MOD;
	}
}

int permutation(int n, int k){
	if(n < k) return 0;
	return fac[n]*mmi[n-k]%MOD;
}

signed main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	cout<<fixed<<setprecision(10);
	
	int N, K;
	int x, y;  
	int sum = 0, sum2 = 0, temp = 0, ans = 0;
	
	cin>>N>>K;
	
	if(N == 1){
		cout<<0<<endl;
		return 0;
	}
	init(N);
	
	exgcd(2,MOD,x,y);
	sum2 = N*(N+1)%MOD*x%MOD;
	sum = sum2*permutation(N,K)%MOD;
	// cout<<sum<<" "<<sum2<<" "<<permutation(N,K)<<endl;
	
	for(int i = 0; i < K; i++){
		int per = permutation(N-i-1,K-i-1)%MOD;
		temp += permutation(N-2,i)*per%MOD*sum2%MOD;
		temp += i*permutation(N-2,i-1)%MOD*per%MOD*N%MOD;
		temp %= MOD;
	}
	
	ans = (sum - temp + MOD)%MOD;
	
	
	cout<<ans<<endl;
	
	return 0;
}
0