http://codeforces.com/contest/552/problem/C
http://www.chenguanghe.com/codeforces-round-308-div-2-c-vanya-and-scales/
http://blog.csdn.net/qq_24451605/article/details/46557713
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.
public void solve(int testNumber, InputReader in, OutputWriter out) {
int w = in.readInt();
int m = in.readInt();
int t = m;//extra
int[] into = new int[50];
int p = 0;
while (m > 0){
into[p] = m % w;
m/=w;
p++;
}
for (int i = 0; i < into.length-2; i++) {
if (into[i] == w) {
into[i] = 0;
into[i+1]++;
}else if (into[i] == w-1) {
into[i+1]++;
}else if (into[i] == 0 || into[i] == 1)
continue;
else {
out.print("NO");
return;
}
}
out.print("YES");
// extra: print the Scales
// ArrayList<Integer> left = new ArrayList<>();
// ArrayList<Integer> right = new ArrayList<>();
// left.add(t);
// for (int i = 0; i < into.length; i++) {
// if (into[i] == 0){
// continue;
// } else if (into[i] == 1) {
// right.add((int)Math.pow(w, i));
// } else if (into[i] == w-1) {
// left.add((int)Math.pow(w, i));
// }
// }
// out.print("left" + left);
// out.print("right" + right);
}
http://blog.csdn.net/qq_24451605/article/details/46557713
这道题给出一个数w,获取它所有的w,w^1,w^2.....,然后给出m,求解 a1*w + a2*w^2 +....... = m ,ai的取值为0,-1,1
题目分析:
首先如果不考虑-1的情况的话,那么ai的取值是1,0,所以我们可以联想到数制转换,然后看每一位是否是0或者1,如果不是,那么减去w,看是不是-1,然后向更高位进位,维护w进制数的合法性,因为该位在不合法情况下只可能调整为-1,所以这种方案能够覆盖所有情况,判断只要出现不能调整的不合法情况就可以判断当前w,m没有可行解
int main ( )
{
while ( ~scanf ( "%d%d" , &w , &m ) )
{
cnt = 0;
while ( m )
{
num[cnt++] = m%w;
m /= w;
}
bool flag = true;
for ( int i = 0 ; i < cnt ; i++ )
{
if ( num[i] == 0 || num[i] == 1 ) continue;
if ( num[i] - w == -1 )
{
num[i+1]++;
int id = i+1;
while ( num[id] >= w )
{
num[id] -= w;
num[id+1]++;
cnt = max ( cnt , id+1 );
id++;
}
continue;
}
flag = false;
}
if ( flag ) puts ("YES");
else puts ("NO");
}
}
https://www.quora.com/What-is-the-logic-of-Vanya-and-Scales-Codeforces-308-div-2-problem
- vector<long> representation;
- void new_div(long a, long w)
- {
- long Q, R;
- Q = a / w;
- R = a % w;
- bool flag=0;
- if(Q == R)
- {
- flag=1;
- }
- if(R > w / 2)
- {
- Q++;
- R = w - R;
- }
- representation.push_back(R);
- if(flag == 1)
- {
- return;
- }
- new_div(Q, w);
- }
- int main()
- {
- long w, m;
- cin >> w >> m;
- new_div(m, w);
- for(unsigned int i = 0; i < representation.size(); i++)
- {
- if(representation[i] != 1 && representation[i] != 0 && representation[i] != -1)
- {
- cout << "NO";
- return 0;
- }
- }
- cout << "YES";
- return 0;
- }