919B - Perfect Number - CodeForces Solution


binary search brute force dp implementation number theory *1100

Please click on ads to support us..

Python Code:

def get_nth_seq(n):
    return 19 + (n - 1) * 9


def perfect_number(num):
    counter = 0
    for i in range(0, 10000000):
        res = get_nth_seq(i)
        calc = res

        total = 0
        while calc > 0:
            total = total + calc % 10
            calc = calc // 10

        if total == 10:
            counter += 1

        if counter == num:
            return res


def main():
    print(perfect_number(int(input())))


if __name__ == '__main__':
    main()

		 		    	 		  		 			 	  	  	 	

C++ Code:

#include<bits/stdc++.h>
using namespace std;

int perfect(int n){
    int ans = 0;
    while(n){
        ans += n%10;
        n/=10;
    }
    return ans;
}

int main(){
    int k;
    cin >> k;
    int num  = 18;
    int n = 0;
    while(n<k){
        num++;
        if(perfect(num) == 10) n++;
    }
    cout << num << endl;
}


Comments

Submit
0 Comments
More Questions

1712C - Sort Zero
1028B - Unnatural Conditions
735B - Urbanization
746C - Tram
1278B - A and B
1353D - Constructing the Array
1269C - Long Beautiful Integer
1076A - Minimizing the String
913C - Party Lemonade
1313A - Fast Food Restaurant
681A - A Good Contest
1585F - Non-equal Neighbours
747A - Display Size
285A - Slightly Decreasing Permutations
515C - Drazil and Factorial
1151E - Number of Components
1151F - Sonya and Informatics
556A - Case of the Zeros and Ones
867A - Between the Offices
1569A - Balanced Substring
260A - Adding Digits
1698C - 3SUM Closure
1029B - Creating the Contest
1421A - XORwice
1029A - Many Equal Substrings
1675D - Vertical Paths
1271C - Shawarma Tent
805A - Fake NP
1163A - Eating Soup
787A - The Monster