Блог пользователя mainnahihoo

Автор mainnahihoo, история, 9 месяцев назад, По-английски

problem atcoder problem

int dp[(1<<20)];
// int cnt=0;
int rec(int mask,string &s,int n){
	// cout<<"call for "<<mask<<endl;
	if(mask==(1<<n)-1)return 1;
	if(dp[mask]!=-1)return dp[mask];
	int x=0;
	for(int i=0;i<n;i++){
		if(!(mask&(1<<i)) && s[mask|(1<<i)]=='0'){
			x|=rec(mask|(1<<i),s,n);
		}
	}
	// cnt++;
	return dp[mask]=x;
}

void solve() {
	// executing code from here 
	int t;
	cin >> t;
	while (t--) {
		int n;cin >> n;string s;cin>>s;
		memset(dp,-1,sizeof(dp));
		s='0'+s;
		if(rec(0,s,n))cy;
		else cn;
	}
}

I am getting TLE why after dp time complexity should be 2^n*n not n!*n

  • Проголосовать: нравится
  • +3
  • Проголосовать: не нравится

»
9 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Auto comment: topic has been updated by mainnahihoo (previous revision, new revision, compare).

»
9 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

the issue is likely that you have return dp[mask]=x;, that should be a double equals sign.

Also, minor spoilers for the problem but

Spoiler
»
9 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

You memset the array once before each test case, for a total complexity of O(T size(dp)), which is clearly the wrong complexity.

»
9 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Don't use memset because it will set -1 to whole dp array which is of 1e6 size(approx) for every testcase which is around 4000 or something, change memset() to for loop of size 2^n