CF-A Sum of 2050

题目链接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;
} 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

嵩韵儿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值