【30.23%】【codeforces 552C】Vanya and Scales
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Vanya has a scales for weighing loads and weights of masses w0,?w1,?w2,?…,?w100 grams where w is some integer not less than 2 (exactly one weight of each nominal value). Vanya wonders whether he can weight an item with mass m using the given weights, if the weights can be put on both pans of the scales. Formally speaking, your task is to determine whether it is possible to place an item of mass m and some weights on the left pan of the scales, and some weights on the right pan of the scales so that the pans of the scales were in balance.
Input
The first line contains two integers w,?m (2?≤?w?≤?109, 1?≤?m?≤?109) — the number defining the masses of the weights and the mass of the item.
Output
Print word ‘YES’ if the item can be weighted and ‘NO’ if it cannot.
Examples
input
3 7
output
YES
input
100 99
output
YES
input
100 50
output
NO
Note
Note to the first sample test. One pan can have an item of mass 7 and a weight of mass 3, and the second pan can have two weights of masses 9 and 1, correspondingly. Then 7?+?3?=?9?+?1.
Note to the second sample test. One pan of the scales can have an item of mass 99 and the weight of mass 1, and the second pan can have the weight of mass 100.
Note to the third sample test. It is impossible to measure the weight of the item in the manner described in the input.
【题目链接】:http://codeforces.com/contest/552/problem/C
【题解】
先将m转换成w进制;
如果最后w进制的每个位都为0或1,则直接输出YES;
对于w进制的m,如果其中的某一位存在w-1,则尝试将w-1变成-1,然后把后一位递增1;
如果某一位中存在2..w-2中的数则无解.
可以用样例的3 7来解释;
7 = 3^0 + 2*3^1;
因为2==3-1
则将2*3^1处理一下
变成(3-1)*3^1
就变成了1*3^2-1*3^1
则
7 = 3^0-3^1+3^2
这时每个数的系数都为1或-1,显然符合题意.
可以看到我们在进行这个转换的时候把第x位系数为w-1变成了-1,然后第x+1位的权值递增了1;即又添加了一个3^2;
那为什么系数为2..w-2的时候不能进行这样的转换?
比如w=4的时候系数为2
比如
2*4^3
这个时候会变成
(4-2)*4^3;
即4^4-2*4^3;
可以看到4^3的系数不会是-1.则不符合题意.因为每种砝码都只能选一次.移到左边后要用两次4^3.
当做技巧吧。不好想。
【完整代码】
#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%I64d",&x)
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
const int MAXN = 100;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
int w,m,cnt = 0;
int a[MAXN];
int main()
{
//freopen("F:\\rush.txt","r",stdin);
rei(w);rei(m);
while (m>0)
{
a[++cnt] = m%w;
m/=w;
}
rep1(i,1,cnt)
{
if (a[i] == w)
{
a[i+1]++;
a[i] = 0;
}
else
if (a[i]==w-1)
{
a[i] = -1;
a[i+1]++;
}
else
if (a[i] == 1 || a[i]==0)
continue;
else
{
puts("NO");
return 0;
}
}
puts("YES");
return 0;
}
【30.23%】【codeforces 552C】Vanya and Scales的更多相关文章
- 【 BowWow and the Timetable CodeForces - 1204A 】【思维】
题目链接 可以发现 十进制4 对应 二进制100 十进制16 对应 二进制10000 十进制64 对应 二进制1000000 可以发现每多两个零,4的次幂就增加1. 用string读入题目给定的二进制 ...
- codeforces 552 C Vanya and Scales
这个题的意思就是给出一个数m.以及一个以1为首元素.w为比例常数的等比数列,数列长度为101,数列中每一个数字最多仅仅能用一次.问是否存在xa+wb+--=wc+wd+--+we+m. 非常显然,换句 ...
- CodeForces 552C Vanya and Scales
Vanya and Scales Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u S ...
- 【20.23%】【codeforces 740A】Alyona and copybooks
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【23.33%】【codeforces 557B】Pasha and Tea
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【30.93%】【codeforces 558E】A Simple Task
time limit per test5 seconds memory limit per test512 megabytes inputstandard input outputstandard o ...
- 【23.39%】【codeforces 558C】Amr and Chemistry
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【30.36%】【codeforces 740D】Alyona and a tree
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【30.49%】【codeforces 569A】Music
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
随机推荐
- POJ 3220 Jessica's Reading Problem
Jessica's Reading Problem Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12944 Accep ...
- [RxJS] Learn How To Use RxJS 5.5 Beta 2
The main changes is about how you import rxjs opreators from now on. And introduce lettable opreator ...
- nyoj999 师傅又被妖怪抓走了 (预处理+bfs+状态压缩)
题目999 题目信息 执行结果 本题排行 讨论区 师傅又被妖怪抓走了 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描写叙述 话说唐僧复得了孙行者,师徒们一心同体,共诣西方.自宝 ...
- 小贝_redis web管理界面工具安装
RedisWEB管理界面工具安装 一.概述 二.文件下载 三.安装过程 一.概述 1.因为redis是基于C/S的方式开发.也就是说,仅仅要满足于redis的client通信要求的,都能够作为redi ...
- Android 监听电量的状态
监控手机电量的变化和充电状态 在BatteryManager中有一个粘性广播,不需要BroadcastReceiver作为接收器,在注册时将传入null IntentFilter filter = n ...
- 当数据库没有备份,redo或undo损坏
数据库在没有备份的情况下,如果数据库redo或undo损坏,可以通过如下方法处理,但是不一定成功 把init文件中的: undo_management=manual 然后启动数据库到mount 状态后 ...
- 如何在anaconda中切换python2
如果你不切换可能是默认的python3环境. 下面是在python27版本下下载qt5
- Loadrunner--Analysis网页细分图
续LR实战之Discuz开源论坛项目,之前一直是创建虚拟用户脚本(Virtual User Generator)和场景(Controller),现在,终于到了LoadRunner性能测试结果分析(An ...
- 使用stringstream进行类型转换与字符串分割
C++标准库中的<sstream>提供了比ANSI C的<stdio.h>更高级的一些功能,即单纯性.类型安全和可扩展性. 如果你已习惯了<stdio.h>风格的转 ...
- 获取iOS顶部状态栏和Navigation的高度
状态栏的高度 20 [[UIApplication sharedApplication] statusBarFrame].size.height Navigation的高度 44 self.navig ...