C. Vanya and Scales
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.
InputThe 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.
OutputPrint 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 NoteNote 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.
?思路:如果是不只一個的話,那么就是轉(zhuǎn)換成k進制。
但是限制了一個,那么將這個數(shù)轉(zhuǎn)化為k進制,由于兩邊都能放,所以這個數(shù)可以看成是兩個數(shù)的差,并且這兩個數(shù)是由0,1組成的,那么就可以轉(zhuǎn)換成當(dāng)前這個數(shù)加上一個只有0,1組成的數(shù)是否能組成另一個全0,1的數(shù),要判斷的這個數(shù)的位上當(dāng)前是0,1就不用加1,也就是
另一個數(shù)這位為0,否則如果等于n-1,就需要加1,否則無論加或不加1都不能得到當(dāng)前位為0,或1,也就不能達到。
1 #include<stdio.h> 2 #include<algorithm> 3 #include<stdlib.h> 4 #include<queue> 5 #include<string.h> 6 #include<iostream> 7 #include<math.h> 8 #include<queue> 9 #include<vector> 10 using namespace std; 11 typedef long long LL; 12 const LL mod = 1e9+7; 13 int ak[100000]; 14 int main(void) 15 { 16 LL n,m; 17 while(scanf("%lld %lld",&n,&m)!=EOF) 18 { 19 int flag = 0; 20 int cn = 0; 21 while(m) 22 { 23 ak[cn++] = m%n; 24 m/=n; 25 } 26 int d = 0; 27 if(n==2)printf("YES\n"); 28 else 29 {for(int i = 0; i < cn; i++) 30 { 31 ak[i]+=d; 32 if(ak[i]==n-1) 33 { 34 ak[i]++; 35 } 36 d = ak[i]/n; 37 ak[i]=ak[i]%n; 38 if(ak[i]!=0&&ak[i]!=1) 39 flag = 1; 40 } 41 if(flag)printf("NO\n"); 42 else printf("YES\n");} 43 } 44 return 0; 45 }代碼庫
轉(zhuǎn)載于:https://www.cnblogs.com/zzuli2sjy/p/5998664.html
總結(jié)
以上是生活随笔為你收集整理的C. Vanya and Scales的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 孕妇梦到蛇一定生男孩吗
- 下一篇: Hello World(本博客启程篇)