結果

問題 No.1102 Remnants
ユーザー furafura
提出日時 2020-07-04 16:44:03
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 99 ms / 2,000 ms
コード長 2,472 bytes
コンパイル時間 2,458 ms
コンパイル使用メモリ 208,372 KB
実行使用メモリ 4,988 KB
最終ジャッジ日時 2023-10-19 08:12:18
合計ジャッジ時間 4,610 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 69 ms
4,724 KB
testcase_06 AC 13 ms
4,348 KB
testcase_07 AC 99 ms
4,988 KB
testcase_08 AC 3 ms
4,348 KB
testcase_09 AC 5 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 4 ms
4,348 KB
testcase_12 AC 6 ms
4,348 KB
testcase_13 AC 5 ms
4,348 KB
testcase_14 AC 40 ms
4,348 KB
testcase_15 AC 31 ms
4,348 KB
testcase_16 AC 80 ms
4,724 KB
testcase_17 AC 76 ms
4,724 KB
testcase_18 AC 78 ms
4,724 KB
testcase_19 AC 27 ms
4,348 KB
testcase_20 AC 10 ms
4,348 KB
testcase_21 AC 53 ms
4,348 KB
testcase_22 AC 75 ms
4,460 KB
testcase_23 AC 57 ms
4,348 KB
testcase_24 AC 40 ms
4,348 KB
testcase_25 AC 91 ms
4,724 KB
testcase_26 AC 6 ms
4,348 KB
testcase_27 AC 25 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;

class mint{
	static const int MOD=1e9+7;
	int x;
public:
	mint():x(0){}
	mint(long long y){ x=y%MOD; if(x<0) x+=MOD; }

	mint& operator+=(const mint& m){ x+=m.x; if(x>=MOD) x-=MOD; return *this; }
	mint& operator-=(const mint& m){ x-=m.x; if(x<   0) x+=MOD; return *this; }
	mint& operator*=(const mint& m){ x=1LL*x*m.x%MOD; return *this; }
	mint& operator/=(const mint& m){ return *this*=inverse(m); }
	mint operator+(const mint& m)const{ return mint(*this)+=m; }
	mint operator-(const mint& m)const{ return mint(*this)-=m; }
	mint operator*(const mint& m)const{ return mint(*this)*=m; }
	mint operator/(const mint& m)const{ return mint(*this)/=m; }
	mint operator-()const{ return mint(-x); }

	friend mint inverse(const mint& m){
		int a=m.x,b=MOD,u=1,v=0;
		while(b>0){ int t=a/b; a-=t*b; swap(a,b); u-=t*v; swap(u,v); }
		return u;
	}

	friend istream& operator>>(istream& is,mint& m){ long long t; is>>t; m=mint(t); return is; }
	friend ostream& operator<<(ostream& os,const mint& m){ return os<<m.x; }
	int to_int()const{ return x; }
};

mint operator+(long long x,const mint& m){ return mint(x)+m; }
mint operator-(long long x,const mint& m){ return mint(x)-m; }
mint operator*(long long x,const mint& m){ return mint(x)*m; }
mint operator/(long long x,const mint& m){ return mint(x)/m; }

mint fact(int n){
	static vector<mint> memo={1};
	if(memo.size()<=n){
		int k=memo.size();
		memo.resize(n+1);
		for(;k<=n;k++) memo[k]=memo[k-1]*k;
	}
	return memo[n];
}

mint fact_inverse(int n){
	static vector<mint> memo={1};
	if(memo.size()<=n){
		int k=memo.size();
		memo.resize(n+1);
		memo[n]=inverse(fact(n));
		for(int i=n;i>k;i--) memo[i-1]=memo[i]*i;
	}
	return memo[n];
}

mint choose(int n,int k,int type=0){
	if(k==0) return 1;
	if(n< k) return 0;
	if(type==0){
		return fact(n)*fact_inverse(k)*fact_inverse(n-k);
	}
	else{
		if(k>n-k) k=n-k;
		mint res=fact_inverse(k);
		rep(i,k) res*=n-i;
		return res;
	}
}

mint multichoose(int n,int k,int type=0){
	return choose(n+k-1,k,type);
}

int main(){
	int n,k; scanf("%d%d",&n,&k);
	vector<int> a(n);
	rep(i,n) scanf("%d",&a[i]);

// TLE
/*
	mint ans=0;
	rep(i,n) ans+=multichoose(i+1,k)*multichoose(n-i,k)*a[i];
	cout<<ans<<'\n';
*/
	mint ans=0,coef=1;
	rep(i,n-1) coef*=n-i+k-1;
	coef/=fact(n-1);
	rep(i,n){
		ans+=coef*a[i];
		coef*=k+i+1;
		coef/=i+1;
		coef/=n-i+k-1;
		coef*=n-i-1;
	}
	cout<<ans<<'\n';

	return 0;
}
0