結果

問題 No.823 Many Shifts Easy
ユーザー okok
提出日時 2019-04-27 00:02:43
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 5 ms / 2,000 ms
コード長 1,480 bytes
コンパイル時間 509 ms
コンパイル使用メモリ 72,540 KB
実行使用メモリ 4,956 KB
最終ジャッジ日時 2023-08-16 18:55:15
合計ジャッジ時間 1,159 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 5 ms
4,956 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 4 ms
4,380 KB
testcase_06 AC 5 ms
4,912 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 3 ms
4,876 KB
testcase_09 AC 1 ms
4,380 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 %= MOD;
		temp += i*permutation(N-2,i-1)%MOD*per%MOD*N%MOD;
		temp %= MOD;
	}
	// cout<<sum<<" "<<temp<<endl;
	ans = (sum - temp + MOD*2)%MOD;
	
	
	cout<<ans<<endl;
	
	return 0;
}
0