結果

問題 No.117 組み合わせの数
ユーザー Ysmr_RyYsmr_Ry
提出日時 2015-01-05 02:24:39
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,139 bytes
コンパイル時間 375 ms
コンパイル使用メモリ 28,548 KB
実行使用メモリ 34,208 KB
最終ジャッジ日時 2023-09-03 22:02:05
合計ジャッジ時間 884 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:50:13: warning: iteration 1999999 invokes undefined behavior [-Waggressive-loop-optimizations]
   fact[i+1] = (i+1)*fact[i] % mod;
   ~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~
main.cpp:4:31: note: within this loop
 #define rep(i,a) for(int i=0;i<(a);++i)
                               ^
main.cpp:49:2: note: in expansion of macro ‘rep’
  rep( i, MAX_N )
  ^~~

ソースコード

diff #

#include<cstdio>
#include<cstring>
#include<cctype>
#define rep(i,a) for(int i=0;i<(a);++i)
#define repd(i,a) for(int i=(a);i>=0;--i)

typedef long long ll;

const ll mod = 1000000007, MAX_N = 2000000;

int T;
ll fact[MAX_N], factinv[MAX_N];

ll extgcd( ll a, ll b, ll &x, ll &y )
{
	int d = b;

	if( b )
	{
		d = extgcd( b, a%b, y, x );
		y -= a/b*x;
	}
	else
		x = 1, y = 0;

	return d;
}

ll divMod( ll a, ll b, ll m )
{
	ll x, y;
	extgcd( b, m, x, y );

	return ((a*x)%m+m)%m;
}

ll perm( int n, int r )
{ return n<r?0:fact[n]*factinv[n-r] % mod; }

ll comb( int n, int r )
{ return n<r?0:perm(n,r)*factinv[r] % mod; }

ll homo( int n, int r )
{ return r?comb( n+r-1, r ):1; }

int main()
{
	fact[0] = 1;
	rep( i, MAX_N )
		fact[i+1] = (i+1)*fact[i] % mod;

	factinv[MAX_N] = divMod( 1, fact[MAX_N], mod );
	repd( i, MAX_N-1 )
		factinv[i] = (factinv[i+1]*(i+1)) % mod;

	scanf( "%d", &T );
	rep( i, T )
	{
		char S[21];
		scanf( "%s", S );

		char buf[21];
		*buf = *S;
		buf[1] = '\0';
		strcat(buf, "(%d,%d)");

		int n, r;		
		sscanf( S, buf, &n, &r );
		printf( "%lld\n", (*S=='C'?comb:*S=='P'?perm:homo)(n, r) );
	}

	return 0;
}
0