Submission #3108185
Source Code Expand
#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define rrep(i,n) for(int i=(int)(n)-1;i>=0;--i)
#define srep(i,s,t) for(int i=(int)(s);i<(int)(t);++i)
#define each(a,b) for(auto& (a): (b))
#define all(v) (v).begin(),(v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)),v.erase(unique(all(v)),v.end())
#define cmx(x,y) x=max(x,y)
#define cmn(x,y) x=min(x,y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout<<#x<<" = "<<(x)<<endl
#define spair(p) cout<<#p<<": "<<p.fi<<" "<<p.se<<endl
#define sar(a,n) cout<<#a<<":";rep(pachico,n)cout<<" "<<a[pachico];cout<<endl
#define svec(v) cout<<#v<<":";rep(pachico,v.size())cout<<" "<<v[pachico];cout<<endl
#define svecp(v) cout<<#v<<":";each(pachico,v)cout<<" {"<<pachico.first<<":"<<pachico.second<<"}";cout<<endl
#define sset(s) cout<<#s<<":";each(pachico,s)cout<<" "<<pachico;cout<<endl
#define smap(m) cout<<#m<<":";each(pachico,m)cout<<" {"<<pachico.first<<":"<<pachico.second<<"}";cout<<endl
using namespace std;
typedef pair<int,int> P;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<P> vp;
typedef vector<string> vs;
const int MAX_N = 11;
ll dp[51][11];
int n, x, p, cnt;
ll res;
vector<vector<int> > ans;
//どこかでmake()することを忘れずに
ll inv[MAX_N],fac[MAX_N],finv[MAX_N];
void make()
{
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for(int i=2;i<MAX_N;i++){
inv[i] = MOD - inv[MOD%i] * (MOD/i) % MOD;
fac[i] = fac[i-1] * (ll) i % MOD;
finv[i] = finv[i-1] * inv[i] % MOD;
}
}
inline ll add(ll x,ll y)
{
return (x + y)%MOD;
}
inline ll mul(ll x,ll y)
{
return x*y%MOD;
}
inline ll calc(vi& hoge)
{
ll tm = 1;
int cnt[11] = {};
each(val,hoge){
cnt[val]++;
}
tm = mul(tm, fac[accumulate(cnt,cnt+11,0)]);
srep(j,1,11){
tm = mul(tm, finv[cnt[j]]);
}
return tm;
}
inline ll mod_pow(ll a,ll b)
{
a %= MOD;
ll res = 1;
while(b){
if(b & 1){
res = res * a % MOD;
}
a = a * a % MOD;
b >>= 1;
}
return res;
}
void dfs(int id, int mn, vi& kp, vi& made)
{
if(id == cnt){
if(kp[x] == 1){
ans.pb(made);
}
return;
}
if(kp[x]){
srep(i,mn,min(x,p)+1){
if(kp[x-i] == 0){
vi nkp = kp;
for(int j = x; j >= 0; j--){
if(j+i<=x && nkp[j]){
nkp[j+i] += nkp[j];
}
}
made.pb(i);
dfs(id+1, i, nkp, made);
made.pop_back();
}
}
if(x < p){
res = add(res, mul(calc(made), mod_pow(p-x,cnt-id)));
}
}else{
srep(i,mn,min(x,p)+1){
if(kp[x-i] == 0){
vi nkp = kp;
for(int j = x; j >= 0; j--){
if(j+i<=x && nkp[j]){
nkp[j+i] += nkp[j];
}
}
made.pb(i);
dfs(id+1, i, nkp, made);
made.pop_back();
}else if(kp[x-i] == 1){
vi nkp = kp;
for(int j = x; j >= 0; j--){
if(j+i<=x && nkp[j]){
nkp[j+i] += nkp[j];
}
}
made.pb(i);
dfs(id+1, i, nkp, made);
made.pop_back();
}
}
}
}
int main()
{
cin.tie(0);
ios::sync_with_stdio(false);
cin >> n >> x >> p;
vi a(n, 0);
vs s(n);
rep(i,n){
cin >> s[i];
if(s[i][0] != '?'){
a[i] = stoi(s[i]);
}else{
cnt++;
}
}
dp[0][0] = 1;
rep(i,n){
if(a[i]){
for(int j = x; j >= 0; j--){
dp[i+1][j] = dp[i][j];
if(j+a[i] <= x && dp[i][j]){
dp[i+1][j+a[i]] += dp[i][j];
}
}
}else{
rep(j,x+1){
dp[i+1][j] = dp[i][j];
}
}
}
if(dp[n][x] > 1){
cout << "0\n";
return 0;
}
vi kp(x+1);
rep(i,x+1){
kp[i] = dp[n][i];
}
vi hoge;
make();
dfs(0, 1, kp, hoge);
zip(ans);
rep(i,len(ans)){
res = add(res, calc(ans[i]));
}
cout << res << "\n";
return 0;
}
Submission Info
Submission Time |
|
Task |
G - 唯一の組み合わせ |
User |
kopricky |
Language |
C++14 (GCC 5.4.1) |
Score |
0 |
Code Size |
4793 Byte |
Status |
WA |
Exec Time |
482 ms |
Memory |
48556 KB |
Judge Result
Set Name |
All |
Score / Max Score |
0 / 200 |
Status |
|
Set Name |
Test Cases |
All |
scrambled_00.txt, scrambled_01.txt, scrambled_02.txt, scrambled_03.txt, scrambled_04.txt, scrambled_05.txt, scrambled_06.txt, scrambled_07.txt, scrambled_08.txt, scrambled_09.txt, scrambled_10.txt, scrambled_11.txt, scrambled_12.txt, scrambled_13.txt, scrambled_14.txt, scrambled_15.txt, scrambled_16.txt, scrambled_17.txt, scrambled_18.txt, scrambled_19.txt, scrambled_20.txt, scrambled_21.txt, scrambled_22.txt, scrambled_23.txt, scrambled_24.txt, scrambled_25.txt, scrambled_26.txt, scrambled_27.txt |
Case Name |
Status |
Exec Time |
Memory |
scrambled_00.txt |
AC |
1 ms |
256 KB |
scrambled_01.txt |
AC |
1 ms |
256 KB |
scrambled_02.txt |
AC |
1 ms |
256 KB |
scrambled_03.txt |
AC |
1 ms |
256 KB |
scrambled_04.txt |
WA |
1 ms |
256 KB |
scrambled_05.txt |
AC |
1 ms |
256 KB |
scrambled_06.txt |
AC |
1 ms |
256 KB |
scrambled_07.txt |
AC |
1 ms |
256 KB |
scrambled_08.txt |
AC |
1 ms |
256 KB |
scrambled_09.txt |
AC |
1 ms |
256 KB |
scrambled_10.txt |
WA |
1 ms |
256 KB |
scrambled_11.txt |
WA |
1 ms |
256 KB |
scrambled_12.txt |
AC |
1 ms |
256 KB |
scrambled_13.txt |
WA |
1 ms |
256 KB |
scrambled_14.txt |
WA |
227 ms |
17712 KB |
scrambled_15.txt |
WA |
2 ms |
256 KB |
scrambled_16.txt |
AC |
1 ms |
256 KB |
scrambled_17.txt |
AC |
1 ms |
256 KB |
scrambled_18.txt |
AC |
1 ms |
256 KB |
scrambled_19.txt |
AC |
1 ms |
256 KB |
scrambled_20.txt |
AC |
1 ms |
256 KB |
scrambled_21.txt |
AC |
1 ms |
256 KB |
scrambled_22.txt |
AC |
1 ms |
256 KB |
scrambled_23.txt |
AC |
1 ms |
256 KB |
scrambled_24.txt |
AC |
1 ms |
256 KB |
scrambled_25.txt |
WA |
3 ms |
384 KB |
scrambled_26.txt |
WA |
2 ms |
384 KB |
scrambled_27.txt |
WA |
482 ms |
48556 KB |