結果

問題 No.1684 Find Brackets
ユーザー kotatsugame
提出日時 2021-09-17 23:08:20
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 31 ms / 2,000 ms
コード長 1,561 bytes
コンパイル時間 729 ms
コンパイル使用メモリ 77,072 KB
実行使用メモリ 15,232 KB
最終ジャッジ日時 2024-06-29 21:54:29
合計ジャッジ時間 1,905 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 21
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp:72:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   72 | main()
      | ^~~~

ソースコード

diff #

#include<iostream>
#include<algorithm>
#include<atcoder/modint>
using namespace std;
#include<vector>
template<typename T>
struct combination{
	vector<T>fac,invfac;
	combination(size_t N=0):fac(1,1),invfac(1,1)
	{
		make_table(N);
	}
	void make_table(size_t N)
	{
		if(fac.size()>N)return;
		size_t now=fac.size();
		N=max(N,now*2);
		fac.resize(N+1);
		invfac.resize(N+1);
		for(size_t i=now;i<=N;i++)fac[i]=fac[i-1]*i;
		invfac[N]=1/fac[N];
		for(size_t i=N;i-->now;)invfac[i]=invfac[i+1]*(i+1);
	}
	T factorial(size_t n)
	{
		make_table(n);
		return fac[n];
	}
	T P(size_t n,size_t k)
	{
		if(n<k)return 0;
		make_table(n);
		return fac[n]*invfac[n-k];
	}
	T C(size_t n,size_t k)
	{
		if(n<k)return 0;
		make_table(n);
		return fac[n]*invfac[n-k]*invfac[k];
	}
	T H(size_t n,size_t k)
	{
		if(n==0)return k==0?1:0;
		return C(n-1+k,k);
	}
};
using mint=atcoder::modint1000000007;
combination<mint>C;
mint sum[1<<20];
mint calc(int N,int M)
{
	mint all=sum[M];
	mint ans=all*N;
	for(int i=1;i<=N-M;i++)
	{
		int t=2*M-N+2*i;
		ans+=sum[N-(N-t)/2];
	}
	ans+=(2*M-N)*all;
	for(int i=2*M-N+1;i<=N;i++)
	{
		ans+=sum[N-(N-i)/2];
		if(i<N)
		{
			//ans+=sum[N-(N-i-1)/2]-sum[N-(N-(2*i-(2*M-N)))/2+1];
			ans+=sum[N-(N-i-1)/2];
			if(N+i-M+1<=N)ans-=sum[N+i-M+1];
		}
	}
	return ans;
}
main()
{
	int N,M;
	cin>>N>>M;
	for(int i=N;i>=0;i--)
	{
		sum[i]=sum[i+1]+C.C(N,i);
	}
	mint ans;
	if(M*2>=N)ans=calc(N,M);
	else
	{
		mint tmp=calc(N,(N+1)/2);
		if(N%2==1)ans=2*tmp;
		else ans=2*tmp-(tmp-calc(N,N/2+1));
		if(M)ans-=calc(N,N-M+1);
	}
	cout<<ans.val()<<endl;
}
0