結果
| 問題 |
No.823 Many Shifts Easy
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2019-04-27 00:02:43 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 6 ms / 2,000 ms |
| コード長 | 1,480 bytes |
| コンパイル時間 | 607 ms |
| コンパイル使用メモリ | 73,268 KB |
| 実行使用メモリ | 6,820 KB |
| 最終ジャッジ日時 | 2024-11-25 08:34:03 |
| 合計ジャッジ時間 | 1,112 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 10 |
ソースコード
#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;
}