这道题难在 hash 上, 求出答案很简单, 关键是我们如何标记, 由于 某个数变换后最多比原数多63 所以我们只需开一个63的bool数组就可以了!
同时注意一下, 可能会有相同的询问。
我为了防止给的询问不是有序的,还排了一边序。
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cmath>
#define N 100
#define M 5010
using namespace std; struct sss
{
int num, place;
}ask[M];
int n, K;
int ans[M];
bool pd[N]={}; bool cmp(sss x, sss y)
{
if (x.num == y.num) return x.place < y.place;
else return x.num < y.num;
} int calc(int now)
{
int a = ;
while (now)
{
a += now % ;
now /= ;
}
return a;
} int main()
{
scanf("%d%d", &n, &K);
for (int i = ; i <= K; ++i)
{
scanf("%d", &ask[i].num);
ask[i].place = i;
}
sort(ask+, ask+K+, cmp);
int num = , nowask = ;
int lastnum = ;
for (int i = ; i <= n; ++i)
{
if (!pd[i%])
{
num++;
while (num == ask[nowask].num)
{
ans[ask[nowask].place] = i;
nowask++;
}
}
pd[i%] = ;
if (i % == )
{
lastnum = calc(i);
pd[(lastnum+i) % ] = ;
}
else
{
lastnum++;
pd[(lastnum+i) % ] = ;
}
}
printf("%d\n", num);
for (int i = ; i < K; ++i)
printf("%d ", ans[i]);
printf("%d\n", ans[K]);
return ;
}