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; }



  • 相关阅读:
    JVM调优-Java中的对象
    Spring MVC如何接收浏览器传递来的请求参数--request--形参--实体类封装
    Navicat for MySQL 64位破解版
    Idea详细配置
    数据库--悲观锁【收藏,待尝试】
    性能问题分析
    java 反射机制之 getDeclaredMethod()获取方法,然后invoke执行实例对应的方法
    Mysql show indexes 查看索引状态
    基于(Redis | Memcache)实现分布式互斥锁
    彻底解决每次打开visio都提示windows正在配置visio的问题
  • 原文地址:https://www.cnblogs.com/bhlsheji/p/5196274.html
Copyright © 2011-2022 走看看