Codeforces Round #308 (Div. 2) C. Vanya and Scales(数制转换)
2015-06-19 11:07
239 查看
http://codeforces.com/contest/552/problem/C
要用质量为w0,w1,…,w100的砝码各1个称出重量m,砝码可以放在天平左边也可以放在右边。问是否可以称出,输出YES或NO。
即w进制的思想
问a1*w^0 + a2*w^1 + a3*w^2 +….+a100*w^100 = m 是否成立的问题
由于每个砝码最多只可以用一个 ,且可以放在两边,则系数a的取值只可能为-1,0,1,
我们考虑某位系数为-1的情况,则说明当前砝码跟物品放在一起,相当于给物品加上了这个砝码的重量。
类似于10进制,依次%W,判断其系数是否为-1,0,1,但是这里注意的一点是%w不可能出现-1,而是出现w-1的情况,当出现这种情况时如前面所说应该讲物品加上当前砝码的质量继续模拟即可
举个例子
1 + 3 - 9 -27 + 81 = 49
49
16 1
5 1
2 2
3 2
0 1
具体看代码:
附上另一种直观的解法:
Basically, let’s try to solve:
c0 + c1*w^1 + c2*w^2 + … = m
Where each c0, c1, c2 are either -1, 0 or 1.
-1 means we used the weight on right, +1 means we used the weight on left and 0 means we did not use the weight at all.
=> c1*w^1 + c2*w^2 + … = m - c0
=> w(c1 + c2*w^1 + c3*w^2 + … ) = m - c0
=> c1 + c2*w^1 + c3*w^2 + … = (m - c0)/w
For such a solution to exist (m — c0) must be a multiple of w.
Let’s divide both sides by w and recursively solve if we can find such an m-c0 where c0 = -1 or 0 or 1
要用质量为w0,w1,…,w100的砝码各1个称出重量m,砝码可以放在天平左边也可以放在右边。问是否可以称出,输出YES或NO。
即w进制的思想
问a1*w^0 + a2*w^1 + a3*w^2 +….+a100*w^100 = m 是否成立的问题
由于每个砝码最多只可以用一个 ,且可以放在两边,则系数a的取值只可能为-1,0,1,
我们考虑某位系数为-1的情况,则说明当前砝码跟物品放在一起,相当于给物品加上了这个砝码的重量。
类似于10进制,依次%W,判断其系数是否为-1,0,1,但是这里注意的一点是%w不可能出现-1,而是出现w-1的情况,当出现这种情况时如前面所说应该讲物品加上当前砝码的质量继续模拟即可
举个例子
1 + 3 - 9 -27 + 81 = 49
49
16 1
5 1
2 2
3 2
0 1
具体看代码:
#include<iostream> #include<limits.h> #include<queue> #include<string.h> #include<cstdio> #include<algorithm> #include<cstring> #define LL long long using namespace std; int main() { int n ,m; cin>>n>>m; if(n==3)//注意w为3的时候能称遍前n项和以内的所有数字,不加这个特判TLE { cout<<"YES"<<endl; return 0; } while(m) { int x = m%n; if(x==n-1) m = m/n + 1; else if(x<=1) m/=n; else { cout<<"NO"<<endl; return 0; } } cout<<"YES"<<endl; return 0; }
附上另一种直观的解法:
Basically, let’s try to solve:
c0 + c1*w^1 + c2*w^2 + … = m
Where each c0, c1, c2 are either -1, 0 or 1.
-1 means we used the weight on right, +1 means we used the weight on left and 0 means we did not use the weight at all.
=> c1*w^1 + c2*w^2 + … = m - c0
=> w(c1 + c2*w^1 + c3*w^2 + … ) = m - c0
=> c1 + c2*w^1 + c3*w^2 + … = (m - c0)/w
For such a solution to exist (m — c0) must be a multiple of w.
Let’s divide both sides by w and recursively solve if we can find such an m-c0 where c0 = -1 or 0 or 1
#include<iostream> #include<limits.h> #include<queue> #include<string.h> #include<cstdio> #include<algorithm> #include<cstring> #define LL long long using namespace std; bool solve(int ,int ); bool trySolve(int w, int m) { return m%w == 0 && solve(w, m/w); } bool solve(int w, int m) { return w == 2 || w == 3 || m == 1 || trySolve(w, m-1) || trySolve(w, m) || trySolve(w, m+1); } int main() { int n ,m; cin>>n>>m; if(solve(n,m)) cout<<"YES"<<endl; else cout<<"NO"<<endl; return 0; }
相关文章推荐
- 用栈实现数制转换
- 数制转换_顺序栈_头文件
- 栈——数制转换
- Why do we use n = 16 * n + hexdigit
- 用堆栈实现数制转换(十进制转二进制)
- 数制转换
- 第15章 位操作 15.2 其他基数
- 数制转换(C语言实现)
- Javascript高级程序设计——3.数据类型(1)typeof、null、undefined、boolean、number
- 用栈的思想实现数制转换(十进制->八进制)
- 任意数制间的转换
- C语言从键盘输入一个十进制数将其转换为任意进制数输出
- 数制转换
- 栈在数制转换上的应用
- 数制转换
- C语言基础篇(一)
- C语言数据结构中数制转换实例代码
- 数据结构 中数制转换(栈的应用)
- JavaScript中各数制转换全面总结
- 关于hive和spark日志问题