D - Snuke Numbers
Time limit : 2sec / Memory limit : 1024MB
Score : 500 points
Problem Statement
Let S(n) denote the sum of the digits in the decimal notation of n. For example, S(123)=1+2+3=6.
We will call an integer n a Snuke number when, for all positive integers m such that m>n,
n |
S(n) |
≤
m |
S(m) |
holds.
Given an integer K, list the K smallest Snuke numbers.
Constraints
- 1≤K
- The K-th smallest Snuke number is not greater than 1015.
Input
Input is given from Standard Input in the following format:
K
Output
Print K lines. The i-th line should contain the i-th smallest Snuke number.
Sample Input 1
Copy
10
Sample Output 1
Copy
1 2 3 4 5 6 7 8 9 19
#include<bits/stdc++.h> using namespace std; typedef long long ll; ll f(ll x){if(x==0) return 0;else return x%10+f(x/10);} int main(){ std::ios::sync_with_stdio(false); int k;cin>>k; int num=0;ll ans=1;ll p=1; while(num<k){ cout<<ans<<endl; ll ans1=ans+p; ll ans2=ans+p*10; if(ans1*f(ans2)<=ans2*f(ans1)){ans=ans1;} else{ans=ans2;p*=10;} num++; } return 0; }