1517A - Sum of 2050 - CodeForces Solution


greedy math *800

Please click on ads to support us..

Python Code:

for ii in range(int(input())):
    n=int(input())
    c=0
    if(n<2050):
        None
    else:
        while len(str(n))>=4:
            x=2050*(10**(len(str(n))-4))
            if(n>=x):
                                n-=x
            else:
                                x=int(x/10)
                                n-=x
            c+=1
    if(n==0):
        print(c)
    else:
        print(-1)

C++ Code:

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

#define ll long long

void solve(){
    ll cur=2050;
    vector <ll> nums;
    ll n;
    cin >> n;
    if (n%2050>0){
        cout << -1 << endl;
        return ;
    }
    while(cur<=n){
        nums.push_back(cur);
        cur*=10;
    }
    reverse(nums.begin(),nums.end());
    int kol=0;
    for (int i =0; i<nums.size(); i++){
        while (n>=nums[i]){
            n-=nums[i];
            kol++;
        }
    }
    if (kol==0) cout << -1<< endl;
    else cout <<kol<< endl;


    return;
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int64_t t;cin >> t;while (t--){solve();}
    

    return 0;
}


Comments

Submit
0 Comments
More Questions

1438A - Specific Tastes of Andre
1711C - Color the Picture
1194C - From S To T
110B - Lucky String
1114A - Got Any Grapes
224B - Array
125B - Simple XML
567B - Berland National Library
431B - Shower Line
282C - XOR and OR
1582B - Luntik and Subsequences
609A - Флеш-карты
1207A - There Are Two Types Of Burgers
371C - Hamburgers
343B - Alternating Current
758B - Blown Garland
1681B - Card Trick
1592A - Gamer Hemose
493D - Vasya and Chess
1485A - Add and Divide
337B - Routine Problem
1392D - Omkar and Bed Wars
76E - Points
762C - Two strings
802M - April Fools' Problem (easy)
577B - Modulo Sum
1555B - Two Tables
1686A - Everything Everywhere All But One
1469B - Red and Blue
1257B - Magic Stick