Codeforces 552C Vanya and Scales(进制转换+思维)
生活随笔
收集整理的這篇文章主要介紹了
Codeforces 552C Vanya and Scales(进制转换+思维)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目鏈接:http://codeforces.com/problemset/problem/552/C
題目大意:
有101個砝碼重量為w^0,w^1,....,w^100和一個重量為m的物體,問能否在天平兩邊放物品和砝碼使其平衡。
解題思路:
將m化為w進制的數,接下來從低到高遍歷沒一位:
如果第i位是0那OK;
如果是1那就要把砝碼wi放在天平另一邊抵消;
如果是w-1那就要把砝碼wi放到天平同一邊,使其變為0并進位,這時第i+1位的權+1;
而如果是其他情況,那么肯定不能平衡了。
代碼:
1 #include<bits/stdc++.h> 2 #define lc(a) (a<<1) 3 #define rc(a) (a<<1|1) 4 #define MID(a,b) ((a+b)>>1) 5 #define fin(name) freopen(name,"r",stdin) 6 #define fout(name) freopen(name,"w",stdout) 7 #define clr(arr,val) memset(arr,val,sizeof(arr)) 8 #define _for(i,start,end) for(int i=start;i<=end;i++) 9 #define FAST_IO ios::sync_with_stdio(false);cin.tie(0); 10 using namespace std; 11 typedef long long LL; 12 const int N=5e6+5; 13 const int INF=0x3f3f3f3f; 14 const double eps=1e-10; 15 16 int a[105]; 17 18 int main(){ 19 FAST_IO; 20 int w,m; 21 cin>>w>>m; 22 int cnt=0; 23 while(m){ 24 a[cnt++]=m%w; 25 m/=w; 26 } 27 bool flag=true; 28 for(int i=0;i<105;i++){ 29 if(a[i]>=w){ 30 a[i+1]+=a[i]/w; 31 a[i]%=w; 32 } 33 if(a[i]!=0){ 34 if(a[i]==w-1) 35 a[i+1]++; 36 else if(a[i]==1) 37 ; 38 else{ 39 flag=false; 40 break; 41 } 42 } 43 } 44 if(flag) 45 cout<<"YES"<<endl; 46 else 47 cout<<"NO"<<endl; 48 return 0; 49 }?
轉載于:https://www.cnblogs.com/fu3638/p/9131476.html
總結
以上是生活随笔為你收集整理的Codeforces 552C Vanya and Scales(进制转换+思维)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: AI量化交易(一)——量化交易简介
- 下一篇: spring 事务控制 设置手动回滚 T