cf#308-C - Vanya and Scales - 数学/进制转换

http://codeforces.com/contest/552/problem/C


给你101个秤砣,重量分别是 w^0,w^1,w^2.....w^100

给你一个m

让你把m放到天枰一端,是否存在一种方案使得放一定数量的秤砣分别在左右两端(可不放)

使得天枰平衡。


思路:进制转换

我们把m转成w进制,得到 m= a*w^k +b*w^(k-1)....+c*w^0


显然 如果 系数c==1或0,我们忽略,否则,要把 c拆分成 w和w-c

例如 如果w=6,系数为w-1=5, 可以把 5*6^3  拆分 (6-1)*6^3= 6^4+6^3

显然,如果系数不是1,0或 w-1,无解


如果系数是w-1则可以进位,我们模拟一下进位拆分过程即可【注意当某一位超过了w,也会进位 】

#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <iostream>
using namespace std;

const double pi=acos(-1.0);
double eps=0.000001; 
int min(int a,int b)
{return a<b?a:b;} 
int max(int a,int b)
{return a>b?a:b;}

int n,m; 
int ans[105];
int deal( )
{
	int i=0;
	while(m)
	{
		ans[i]=m%n;
		m/=n;
		i++;
	}
	return i;
} 
set<int >sb;
int main()
{
	   
	int i;
	cin>>n>>m; 
	int ret=	deal( );
	int flag=0; 
	for (i=0;i<ret;i++)
	{
	 if (ans[i]<=1) continue;
	 else if (ans[i]==n-1)
	 {
		 ans[i]=0;
		 ans[i+1]++;
	 }
	 else
		 if (ans[i]>=n)
		 {
			 ans[i]-=n;
			 ans[i+1]++;
			 i--;continue;
		 }
		 else{flag=1;break;}
	}
	if (!flag)printf("YES\n");
	else printf("NO\n"); 
	return 0;
	
}


你可能感兴趣的:(cf#308-C - Vanya and Scales - 数学/进制转换)