H y p e r l i n k Hyperlink Hyperlink
https://www.luogu.com.cn/problem/U102373?contestId=25686
D e s c r i p t i o n Description Description
给定一个数 n n n,求满足 x + c o u n t ( x ) = n x+count(x)=n x+count(x)=n的所有正整数 x x x
n ≤ 1 0 6 n\leq 10^6 n≤106
S o l u t i o n Solution Solution
注意到 0 < c o u n t ( x ) < 81 0<count(x)<81 0<count(x)<81,直接往前找81个即可
时间复杂度: O ( 729 ) O(729) O(729)
C o d e Code Code
#include<cctype>
#include<cstdio>
#include<algorithm>
#define LL long long
using namespace std;LL n,res,a[100010];
inline LL read()
{
char c;LL d=1,f=0;
while(c=getchar(),!isdigit(c)) if(c=='-') d=-1;f=(f<<3)+(f<<1)+c-48;
while(c=getchar(),isdigit(c)) f=(f<<3)+(f<<1)+c-48;
return d*f;
}
signed main()
{
n=read();
for(LL i=max(n-81,1ll);i<=n;i++)
{
LL x=i,y=0;
while(x) y+=x%10,x/=10;
if(i+y==n) a[++res]=i;
}
printf("%lld\n",res);
for(register int i=1;i<=res;i++) printf("%lld\n",a[i]);
}