Help me in solving SUPINC problem

My issue

In this question, why did my logic fail?

include <bits/stdc++.h>
using namespace std;

int main() {
// your code goes here
long long t,n,k,x;
cin >> t;
while(t–){
cin >> n >> k >> x;
long long sum = 0, total = 1;
for(long long i = 1; i < k; i++){
sum += total;
total = sum + 1;
}
if(total <= x) cout << “yes” << endl;
else cout << “no” << endl;
}
}

My code

#include <bits/stdc++.h>
using namespace std;

int main() {
	// your code goes here
	long long t,n,k,x;
	cin >> t;
	while(t--){
	    cin >> n >> k >> x;
	    long long sum = 0, total = 1;
	    for(long long i = 1; i < k; i++){
	        sum += total;
	        total = sum + 1;
	    }
	    if(total <= x) cout << "yes" << endl;
	    else cout << "no" << endl;
	}
}

Problem Link: Superincreasing Practice Coding Problem - CodeChef

You long longs overflow at i = 64.

Try another approach.

1 Like

I solved this problem by very similar approch and i think you should not be using line total = sum + 1;
because we need not want to do so
just see its forming a series if u focus the minimum most elements that places can take