codeforces 552C Vanya and Scales(进制转化)

题目链接:http://codeforces.com/problemset/problem/552/C

题意:

现有w^0, w^1, w^2……w^100克的秤砣

给出w,m

问能不能用上诉的秤砣算出m,秤砣可以放天平的左右两边,每个秤砣只能用一次

可以就输出YES,不可以就输出NO

比如:

输入3   7

3+7 = 9+1

输出YES


解题思路:

先看一个特殊情况:

如果输入3  7

先把7转化为3进制,是21    

7 = 2*3^1+1*3^0 = (3-1)*3^1+1*3^0 = 1*3^2-1*3^1+1*3^0

减号表示和7放同侧,+表示放异侧

结果就是7+3 = 9+1

所以先将m变成w进制,1表示与m异侧,w-1表示同侧,若出现其他数就输出NO


#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
int p[105];
int main()
{
	int w,n;
	scanf("%d%d",&w,&n);
	int l=0;
	while(n)
	{
		p[l] = n%w;
		n/=w;
		l++;
	}
	if(l>101) 
	{
		printf("NO\n");
		return 0;
	}
	for(int i=0;i<l;i++)
	{
		if(p[i]>=w)
		{
			p[i]-=w;
			p[i+1]++;
		}
		if(p[i]<=1) continue;
		else if(p[i]==w-1)
		{
			p[i]=0;
			p[i+1]++;
		}
		else 
		{
			printf("NO\n");
			return 0;
		}
	}
	printf("YES\n");
	return 0;
} 




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值