zoukankan      html  css  js  c++  java
  • CF 552C 进制转换

    http://codeforces.com/problemset/problem/552/C

    C. Vanya and Scales
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard 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 ≤ 1091 ≤ 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.

    Sample test(s)
    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.


    /**
    CF 552C 进制转换
    题目大意:给定一个天平。砝码的重量为w的0~100次幂。每种砝码仅仅有一个,砝码能够放在左盘或者右盘。

    给定物品的重量m。问是否有一种方案让天平两端平衡 解题思路:把m化为w进制。改进制数仅仅能有0,1或者w-1。若为w-1那么相当于在物品所放的盘里加一个砝码,然后在还有一盘加上w倍的砝码就可以。

    直接w进制数当前位 清0,将下一位+1就可以。最后看w进制数是否正好是一个01串就可以 */ #include <stdio.h> #include <algorithm> #include <string.h> #include <iostream> using namespace std; int bit[40],n,m; int main() { while(~scanf("%d%d",&n,&m)) { int k=0; memset(bit,0,sizeof(bit)); while(m) { bit[k++]=m%n; m/=n; } int flag=1; for(int i=0;i<k;i++) { if(bit[i]>=n) { bit[i]-=n; bit[i+1]++; } if(bit[i]==n-1) { bit[i]=0; bit[i+1]++; } else if(bit[i]>1) { flag=0; break; } } if(flag==0) puts("NO"); else puts("YES"); } return 0; }



  • 相关阅读:
    python:linux中升级python版本
    robot:当用例失败时执行关键字(发送短信)
    robot:根据条件主动判定用例失败或者通过
    robot:List变量的使用注意点
    python:动态参数*args
    robot:linux下安装robot环境
    robot:循环遍历数据库查询结果是否满足要求
    爬虫结果数据完整性校验
    ChromeDriver与chrome对应关系
    Spring系列之AOP
  • 原文地址:https://www.cnblogs.com/bhlsheji/p/5196274.html
Copyright © 2011-2022 走看看