AtCoder Beginner Contest 182-----C(思维题,类似整数相加,取模)

 

C - To 3 Editorial / 


Time Limit: 2 sec / Memory Limit: 1024 MB

Score : 300300 points

Problem Statement

Given is a positive integer NN, where none of the digits is 00.
Let kk be the number of digits in NN. We want to make a multiple of 33 by erasing at least 00 and at most k−1k−1 digits from NN and concatenating the remaining digits without changing the order.
Determine whether it is possible to make a multiple of 33 in this way. If it is possible, find the minimum number of digits that must be erased to make such a number.

Constraints

  • 1≤N<10181≤N<1018
  • None of the digits in NN is 00.

Input

Input is given from Standard Input in the following format:

NN

Output

If it is impossible to make a multiple of 33, print -1; otherwise, print the minimum number of digits that must be erased to make such a number.


Sample Input 1 Copy

Copy

35

Sample Output 1 Copy

Copy

1

By erasing the 55, we get the number 33, which is a multiple of 33. Here we erased the minimum possible number of digits - 11.


Sample Input 2 Copy

Copy

369

Sample Output 2 Copy

Copy

0

Note that we can choose to erase no digit.


Sample Input 3 Copy

Copy

6227384

Sample Output 3 Copy

Copy

1

For example, by erasing the 88, we get the number 622734622734, which is a multiple of 33.


Sample Input 4 Copy

Copy

11

Sample Output 4 Copy

Copy

-1

Note that we must erase at least 00 and at most k−1k−1 digits, where kk is the number of digits in NN, so we cannot erase all the digits.
In this case, it is impossible to make a multiple of 33 in the way described in the problem statement, so we should print -1.

思路:
每一位对3取模,记录下来,去掉的话只能去掉1,2,0不用去。

sum % 3 == 0输出0

sum <= 2直接输出-1

枚举去掉1和2的个数,取最小

边界条件的处理:

全去光1,2,只要0个数不为0就行

输出最小值

code:
 

#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
#define N 100003
#define mod 100000
#define pb push_back
#define x first
#define y second
#define ull unsigned long long
#define ll long long
using namespace std;
int a[3];

int main()
{
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    memset(a,0,sizeof(a));
	ll n;
	cin >> n;
	int sum = 0;
	while(n){
		sum += n % 10;
		a[(n % 10) % 3] ++;
		n /= 10;
	}
	if(sum <= 2)
	{
		cout << -1 << endl;
	}
	else if(sum % 3 == 0)
	{
		cout << 0 << endl;
	}
	else
	{
		int res = a[1] + a[2];
		for(int i = 0;i <= a[1];i++)
		{
			for(int j = 0;j <= a[2];j++)
			{
				if(i == 0 && j == 0)
				continue;
				if((1 * (a[1] - i) + 2 * (a[2] - j) ) % 3 == 0)
				{
					res = min(res,i + j);
				}
			}
		}
		if(res == a[1] + a[2] && a[0] == 0)
		cout << -1 <<endl;
		else
		cout << res << endl;
	}
    return 0;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值