109A - Lucky Sum of Digits - CodeForces Solution


brute force implementation *1000

Please click on ads to support us..

Python Code:

n = int(input())

for i in range(n // 7 + 1):
    for j in range((n - 7 * (n // 7 - i)) // 4 + 1):
        if (n // 7 - i) * 7 + j * 4 == n:
            print("4" * j + "7" * (n // 7 - i))
            quit()
        elif (n // 7 - i) * 7 + j * 4 > n:
            break
print(-1)

C++ Code:

#include <bits/stdc++.h>
 
using namespace std;
 
int main()
{
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
        int n;
    cin>>n;
    string ans = "";
    while(n >= 7 && n%4 != 0){
        n-=7;
        ans+='7';
    }
    if(n%4 != 0){
        cout<<-1<<'\n';
        exit(0);
    }
    while(n >= 28){
        n-=28;
        ans+="7777";
    }
    while(n != 0){
        n-=4;
        ans+='4';
    }
    sort(ans.begin(), ans.end());
    cout<<ans<<'\n';
}


Comments

Submit
0 Comments
More Questions

810A - Straight A
1433C - Dominant Piranha
633A - Ebony and Ivory
1196A - Three Piles of Candies
299A - Ksusha and Array
448B - Suffix Structures
1092B - Teams Forming
1166C - A Tale of Two Lands
544B - Sea and Islands
152B - Steps
1174D - Ehab and the Expected XOR Problem
1511A - Review Site
1316A - Grade Allocation
838A - Binary Blocks
1515D - Phoenix and Socks
1624D - Palindromes Coloring
1552F - Telepanting
1692G - 2Sort
1191A - Tokitsukaze and Enhancement
903A - Hungry Student Problem
52B - Right Triangles
1712A - Wonderful Permutation
1712D - Empty Graph
1712B - Woeful Permutation
1712C - Sort Zero
1028B - Unnatural Conditions
735B - Urbanization
746C - Tram
1278B - A and B
1353D - Constructing the Array