Getting TLE....is this problem solvable with bitmask?? i counted current position and previous combination of (1,2) as my dp state.Here is my code.
Code: Select all
#include<cstdio>
#include<sstream>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<algorithm>
#include<set>
#include<queue>
#include<stack>
#include<list>
#include<iostream>
#include<fstream>
#include<numeric>
#include<string>
#include<vector>
#include<cstring>
#include<map>
#include<iterator>
#include<limits>
#include<iomanip>
#define inf 4611686018427387904
#define Max(v) *max_element(v.begin(),v.end())
#define Min(v) *min_element(v.begin(),v.end())
#define inp1(x) scanf("%d",&x)
#define inp2(x,y) scanf("%d %d",&x,&y)
#define Unique(v) v.resize(unique(v.begin(),v.end())-v.begin())
#define Sort(v) sort(v.begin(),v.end(),greater<int>());
#define fread() freopen("inp.txt","r",stdin)
#define fwrite() freopen("out.txt","w",stdout)
#define mem(n,m) memset(n,m,sizeof n)
int Set(int N,int pos){return N=N | (1<<pos);}
int reset(int N,int pos){return N= N & ~(1<<pos);}
bool check(int N,int pos){return (bool)(N & (1<<pos));}
int cnt_leading_zero_bits(int N){return __builtin_clz(N);}
int cnt_trailing_zero_bits(int N){return __builtin_ctz(N);}
int cnt_no_of_bits_on(int N){return __builtin_popcount(N);}
//priority_queue< int, vector<int>, greater<int> > PQ;// keeps in ascending order
// bool operator < ( const node& b ) const
using namespace std;
long long dp_mn[18][1<<18],dp_mx[18][1<<18],p,q;
bool visited[18][1<<18];
pair<long long,long long> solve(int no,int msk,long long val)
{
if(no>p)
{
if(val%q==0&&val!=0)
{
return make_pair(val,val);
}
else
return make_pair(inf,-1);
}
if(visited[no][msk])
return make_pair(dp_mn[no][msk],dp_mx[no][msk]);
visited[no][msk]=true;
pair<long long,long long>a,b,c;
int msk2=Set(msk,no);
long long one,two;
one=val+pow(10,no-1);
two=val+2*pow(10,no-1);
a=solve(no+1,msk,one);
b=solve(no+1,msk2,two);
if(a.first==-1)
dp_mn[no][msk]=b.first;
else if(b.first==-1)
dp_mn[no][msk]=a.first;
else
dp_mn[no][msk]=min(a.first,b.first);
if(a.second==inf)
dp_mx[no][msk]=b.second;
else if(b.second==inf)
dp_mx[no][msk]=a.second;
else
dp_mx[no][msk]=max(a.second,b.second);
c.first=dp_mn[no][msk];
c.second=dp_mx[no][msk];
return c;
}
int main()
{
int i,j,k,m,n,t,cs=0;
cin>>t;
while(t--)
{
cs++;
cin>>p>>q;
q=pow(2,q);
for(i=0;i<=p;i++)
{
k=pow(2,i);
for(j=0;j<=k;j++)
visited[i][j]=false;
}
//mem(visited,false);
//mem(dp,-1);
//inp2(p,q);
pair<long long,long long>pa;
pa=solve(1,0,0);
cout<<"Case "<<cs<<": ";
if(pa.first==inf)
cout<<"impossible\n";
else
{
if(pa.first==pa.second)
{
cout<<pa.first<<endl;
}
else
{
cout<<pa.first<<" "<<pa.second<<endl;
}
}
}
return 0;
}