P7829 [CCO2021] Weird Numeral System 题解
代码出现神秘错误给我整破防了。
其实这题暴力 DP 就行了。
做法不会证明时间复杂度,先别急。
还有就是要小心
还有一点就是题目对于
有的小朋友说,不可以特判吗?你写写,想想,就算你把选
#include<bits/stdc++.h>
#define LL long long
#define LF long double
#define pLL pair<LL,LL>
#define pb push_back
//#define fir first
//#define sec second
using namespace std;
//const LL inf;
const LL N=1e3+5;
//const LL M;
//const LL mod;
//const LF eps;
//const LL P;
int k,q,d,m,a[N];
unordered_map<LL,bool>ma,A;
vector<int>ans;
bool dfs(LL x)
{
if(ma.count(x))return ma[x];
ma[x]=0;
if(A.count(x))
{
ans.pb(x);
return 1;
}
for(int i=1;i<=d;i++)
{
if((x-a[i])%k==0&&(x-a[i])/k!=x)
if(dfs((x-a[i])/k))
{
ans.pb(a[i]);
return 1;
}
}
return 0;
}
int main()
{
scanf("%d%d%d%d",&k,&q,&d,&m);
for(int i=1;i<=d;i++)
{
scanf("%d",&a[i]);
A[a[i]]=1;
}
while(q--)
{
LL x;
scanf("%lld",&x);
ma.clear(),ans.clear();
if(!dfs(x))
{
puts("IMPOSSIBLE");
continue;
}
else
{
for(LL i:ans)
{
printf("%lld ",i);
}
printf("\n");
}
}
return 0;
}
//RP++