题目链接Contest 2050 and Codeforces Round #718 (Div. 1 + Div. 2)
A number is called 2050-number if it is 2050, 20500, ..., (2050⋅10k for integer k≥0).
Given a number n, you are asked to represent n as the sum of some (not necessarily distinct) 2050-numbers. Compute the minimum number of 2050-numbers required for that.
Input
The first line contains a single integer T (1≤T≤1000) denoting the number of test cases.
The only line of each test case contains a single integer n (1≤n≤1018) denoting the number to be represented.
Output
For each test case, output the minimum number of 2050-numbers in one line.
If n cannot be represented as the sum of 2050-numbers, output −1 instead.
Example
Input
6
205
2050
4100
20500
22550
25308639900
Output
-1
1
2
1
2
36
Note
In the third case, 4100=2050+2050.
In the fifth case, 22550=20500+2050.
Analysis:
t组数据,每组一个n (1≤n≤1018),输出n中含有最少的“2050-number”方案的数量,若n不能被分解,output -1.
Solution:
1-->2050整除n,然后每一位的数相加..
2-->看成2050*10^k,n依次减去最大的2050数...
Pay attention:
pow()..开成 long long 数据类型...
Code one:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
void solve(ll n)
{
if(n%2050)
{
printf("-1\n");
return ;
}
ll x=n/2050;
ll ans=0;
while(x)
{
ans+=x%10;
x/=10;
}
printf("%lld\n",ans);
return ;
}
int main()
{
int t;
cin>>t;
while(t--)
{
ll n;
cin>>n;
solve(n);
}
return 0;
}
Code two:
#include<bits/stdc++.h>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
using namespace std;
typedef long long ll;
ll number(ll x)
{
ll ans=0;
while(x)
{
ans++;
x/=10;
}
return ans;
}
int main()
{
ll t;
scanf("%lld",&t);
while(t--)
{
ll x;
scanf("%lld",&x);
if(x<2050)
{
printf("-1\n");
}
else
{
ll a=number(x);
ll sum=0;
ll aa=4;
while(x>=2050)
{
while(x>=(ll)pow(10,(a-aa))*2050) ///开long long
{
sum++;
x-=(ll)pow(10,(a-aa))*2050; ///开long long
}
if(aa<a)aa++;
// cout<<x<<"++"<<endl;
}
if(x)printf("-1\n");
else
printf("%lld\n",sum);
}
}
return 0;
}