Submission #867462

# Submission time Handle Problem Language Result Execution time Memory
867462 2023-10-28T12:39:28 Z epicci23 Magneti (COCI21_magneti) C++17
110 / 110
721 ms 26460 KB
#include "bits/stdc++.h"
using namespace std;
#define pb push_back
#define endl "\n" 
#define int long long
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()


constexpr int MOD = (int) 1e9 + 7;

inline int add(int a,int b){
  if(a+b>=MOD) return a+b-MOD;
  return a+b;
}

inline int mult(int a,int b){
  if(a*b>=MOD) return (a*b)%MOD;
  return a*b;
}

inline int fast(int a,int b){
  if(b==0) return 1;
  int res=fast(a,b/2);
  res=mult(res,res);
  if(b&1) res=mult(res,a);
  return res;
}

void solve(){

  int n,l;
  cin >> n >> l;
  
  vector<int> ar;

  int fact[10005];
  int inv_fact[10005];
  fact[0]=1;
  for(int i=1;i<10005;i++) fact[i]=mult(i,fact[i-1]);
  
  inv_fact[10004]=fast(fact[10004],MOD-2);

  for(int i=10003;i>=0;i--) inv_fact[i]=mult(inv_fact[i+1],i+1);

  for(int i=1;i<=n;i++){
  	int a;
  	cin >> a;
  	ar.pb(a);
  }

  if(n==1){
  	cout << l << endl;
  	return;
  }

  sort(all(ar));
  reverse(all(ar));

  int dp[n+5][l+5][3];
  int ndp[n+5][l+5][3];
  
  memset(dp,0,sizeof(dp));
  memset(ndp,0,sizeof(ndp));

  dp[0][0][0]=1;

  for(int i=0;i<n;i++){

  	// merge 
    for(int j=2;j<=n;j++){
       for(int x=0;x<=l;x++){
         for(int fl=0;fl<=2;fl++){
           if(x+1<=l) ndp[j-1][x+1][fl]=add(ndp[j-1][x+1][fl],mult(j-1,dp[j][x][fl]));
         }
       }
    }

   	// extend
    for(int j=1;j<=n;j++){
       for(int x=0;x<=l;x++){
         for(int fl=0;fl<=2;fl++){
           if(x+ar[i]<=l) ndp[j][x+ar[i]][fl]=add(ndp[j][x+ar[i]][fl],mult(2*j-fl,dp[j][x][fl]));
           if(fl+1<=2 && x+1<=l) ndp[j][x+1][fl+1]=add(ndp[j][x+1][fl+1],mult(2-fl,dp[j][x][fl]));
         }
       }
    }
  
  	// create1
  	for(int j=0;j<n;j++){
      for(int x=0;x+2*ar[i]-1<=l;x++){
      	for(int fl=0;fl<=2;fl++){
          ndp[j+1][x+2*ar[i]-1][fl]=add(ndp[j+1][x+2*ar[i]-1][fl],mult(j+1-fl,dp[j][x][fl]));
      	}
      }
  	}

  	// create2
    for(int j=0;j<n;j++){
      for(int x=0;x+ar[i]<=l;x++){
      	for(int fl=0;fl<2;fl++){
          ndp[j+1][x+ar[i]][fl+1]=add(ndp[j+1][x+ar[i]][fl+1],mult(2-fl,dp[j][x][fl]));
      	}
      }
  	}

  	//swap values

    memset(dp,0,sizeof(dp));

  	for(int j=0;j<=n;j++)
      for(int x=0;x<=l;x++)
      	for(int fl=0;fl<=2;fl++)
          dp[j][x][fl]=ndp[j][x][fl];

  	memset(ndp,0,sizeof(ndp));
  }
  
  int ans=0;
  for(int x=0;x<=l;x++){
  	ans=add(ans,mult(mult(fact[n+l-x],mult(inv_fact[n],inv_fact[l-x])),dp[1][x][2]));
  }

  cout << ans << endl;
}

int32_t main(){

  cin.tie(0); ios::sync_with_stdio(0);
  
  int t=1;//cin >> t;
  while(t--) solve();

  return 0;
}	

Compilation message

Main.cpp: In function 'void solve()':
Main.cpp:112:7: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
  112 |       for(int x=0;x<=l;x++)
      |       ^~~
Main.cpp:116:4: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
  116 |    memset(ndp,0,sizeof(ndp));
      |    ^~~~~~
# Verdict Execution time Memory Grader output
1 Correct 667 ms 26428 KB Output is correct
2 Correct 5 ms 860 KB Output is correct
3 Correct 24 ms 6748 KB Output is correct
4 Correct 6 ms 4696 KB Output is correct
5 Correct 27 ms 4188 KB Output is correct
6 Correct 165 ms 14684 KB Output is correct
7 Correct 192 ms 13912 KB Output is correct
8 Correct 0 ms 604 KB Output is correct
9 Correct 85 ms 9304 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 7512 KB Output is correct
2 Correct 2 ms 2396 KB Output is correct
3 Correct 24 ms 7652 KB Output is correct
4 Correct 4 ms 3672 KB Output is correct
5 Correct 23 ms 7516 KB Output is correct
6 Correct 4 ms 2652 KB Output is correct
7 Correct 23 ms 7648 KB Output is correct
8 Correct 3 ms 1116 KB Output is correct
9 Correct 4 ms 2908 KB Output is correct
10 Correct 0 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 860 KB Output is correct
2 Correct 2 ms 860 KB Output is correct
3 Correct 7 ms 1116 KB Output is correct
4 Correct 3 ms 868 KB Output is correct
5 Correct 6 ms 860 KB Output is correct
6 Correct 1 ms 856 KB Output is correct
7 Correct 6 ms 868 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 2 ms 924 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 667 ms 26428 KB Output is correct
2 Correct 5 ms 860 KB Output is correct
3 Correct 24 ms 6748 KB Output is correct
4 Correct 6 ms 4696 KB Output is correct
5 Correct 27 ms 4188 KB Output is correct
6 Correct 165 ms 14684 KB Output is correct
7 Correct 192 ms 13912 KB Output is correct
8 Correct 0 ms 604 KB Output is correct
9 Correct 85 ms 9304 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
11 Correct 23 ms 7512 KB Output is correct
12 Correct 2 ms 2396 KB Output is correct
13 Correct 24 ms 7652 KB Output is correct
14 Correct 4 ms 3672 KB Output is correct
15 Correct 23 ms 7516 KB Output is correct
16 Correct 4 ms 2652 KB Output is correct
17 Correct 23 ms 7648 KB Output is correct
18 Correct 3 ms 1116 KB Output is correct
19 Correct 4 ms 2908 KB Output is correct
20 Correct 0 ms 604 KB Output is correct
21 Correct 6 ms 860 KB Output is correct
22 Correct 2 ms 860 KB Output is correct
23 Correct 7 ms 1116 KB Output is correct
24 Correct 3 ms 868 KB Output is correct
25 Correct 6 ms 860 KB Output is correct
26 Correct 1 ms 856 KB Output is correct
27 Correct 6 ms 868 KB Output is correct
28 Correct 1 ms 604 KB Output is correct
29 Correct 2 ms 924 KB Output is correct
30 Correct 1 ms 604 KB Output is correct
31 Correct 711 ms 26420 KB Output is correct
32 Correct 388 ms 17756 KB Output is correct
33 Correct 701 ms 26428 KB Output is correct
34 Correct 120 ms 10876 KB Output is correct
35 Correct 686 ms 26204 KB Output is correct
36 Correct 41 ms 6996 KB Output is correct
37 Correct 721 ms 26200 KB Output is correct
38 Correct 89 ms 4860 KB Output is correct
39 Correct 668 ms 26428 KB Output is correct
40 Correct 178 ms 14940 KB Output is correct
41 Correct 662 ms 26432 KB Output is correct
42 Correct 4 ms 1884 KB Output is correct
43 Correct 659 ms 26204 KB Output is correct
44 Correct 54 ms 7772 KB Output is correct
45 Correct 668 ms 26460 KB Output is correct
46 Correct 3 ms 3672 KB Output is correct
47 Correct 125 ms 13468 KB Output is correct
48 Correct 47 ms 4952 KB Output is correct
49 Correct 9 ms 5272 KB Output is correct
50 Correct 1 ms 856 KB Output is correct