C. Vanya and Scales

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 ≤ 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.

Examples
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.

 思路:如果是不只一个的话,那么就是转换成k进制。

但是限制了一个,那么将这个数转化为k进制,由于两边都能放,所以这个数可以看成是两个数的差,并且这两个数是由0,1组成的,那么就可以转换成当前这个数加上一个只有0,1组成的数是否能组成另一个全0,1的数,要判断的这个数的位上当前是0,1就不用加1,也就是

另一个数这位为0,否则如果等于n-1,就需要加1,否则无论加或不加1都不能得到当前位为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 }

代码库

转载于:https://www.cnblogs.com/zzuli2sjy/p/5998664.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/402768.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

js_组合继承(最常用的继承方式)

<script type"text/javascript">//组合继承: 将原型链和借用构造函数的技术组合在一起//使用原型链实现对原型方法的继承//使用构造函数实现对实例属性的继承//js中最常用的继承方式//super:超类function SuperType(name) {//超类属性//使用构造函数实现对实例…

Hello World(本博客启程篇)

Hello World 作为本博客第一篇日志&#xff0c;作为程序员&#xff0c;无论走到哪里&#xff0c;做什么事&#xff0c;必须先输出这句话。 一个想法  从今天3月份到现在一直在学技术&#xff0c;过程中坑的解决、知识的总结以及想法等都写到了“有道云笔记”。 写有道云笔记的…

php7.2 event扩展php_sockets_le_socket错误解决

环境: [rootlocalhost php.d]# cat /etc/redhat-release CentOS Linux release 7.4.1708 (Core) [rootlocalhost php.d]# php -v PHP 7.2.10 (cli) (built: Sep 15 2018 07:10:58) ( NTS ) Copyright (c) 1997-2018 The PHP Group Zend Engine v3.2.0, Copyright (c) 1998-201…

NYOJ_37_回文字符串

首先&#xff0c;我们把字符串反转&#xff0c;然后用反串和原串求最大公共子序列&#xff0c;再用字符串长度减去最大公共子序列的长度就是答案&#xff0c;我们还可以用滚动数组优化内存 状态转移方程&#xff1a; (i长度的a串和j长度的b串的最长公共子序列长度&#xff09; …

js中的Object.create(null) 和 {} 的区别

console.log(Object.create(null))console.log({})结果图 根据结果我们不难发现&#xff0c;两者最大区别在于Object.create(null)创建的对象没有继承Object 所有Object.create(null)创建的对象也没有Object提供的方法&#xff0c;是一个非常纯净的对象 ps: Object.create(obj…

matplotlib —— 添加文本信息(text)

matplotlib.pyplot.text(x, y, s, fontdictNone, withdashFalse, **kwargs) x, y&#xff1a;表示坐标&#xff1b;s&#xff1a;字符串文本&#xff1b;fontdict&#xff1a;字典&#xff0c;可选&#xff1b;kw&#xff1a; fontsize12,horizontalalignment‘center’、ha’c…

值得推荐的C/C++框架和库 (真的很强大)

From: https://blog.csdn.net/u011628751/article/details/69258637/ 值得学习的C语言开源项目 - 1. Webbench Webbench是一个在Linux下使用的非常简单的网站压测工具。它使用fork()模拟多个客户端同时访问我们设定的URL&#xff0c;测试网站在压力下工作的性能&#xff0c;…

java web 中的转发和重定向路径问题

为什么80%的码农都做不了架构师&#xff1f;>>> 假设应用程序的 contextPath 为 /ctx&#xff0c;在 http://localhost:8080/ctx/a/b 资源中&#xff0c;我们转发和重定向到 http://localhost:8080/ctx/x/y 资源&#xff0c;分别应该怎么写&#xff1f; 转发&…

断言assert使用方法

测试代码 console.log(开始)console.assert(true, true)console.assert(false,false)console.log(结束)结果 可以发现&#xff1a; assert方法中&#xff0c;第一个参数是判断条件&#xff0c;当为true时&#xff0c;不执行 。当为false时&#xff0c; 输出后面提示内容 且我们…

AngularJs学习笔记(四)

1、AngularJs中的Module可以包含哪些组件&#xff1f;&#xff08;1&#xff09;value&#xff1a;保存可以在不同的controller之间共享的对象、数据&#xff08;2&#xff09;constant&#xff1a;保存可以在不同的controller之间共享的常量&#xff08;3&#xff09;service&…

[你必须知道的异步编程]——异步编程模型(APM)

本专题概要&#xff1a;引言你知道APM吗&#xff1f;你想知道如何使用异步编程模型编写代码吗&#xff1f;使用委托也可以实现异步编程&#xff0c;你知道否&#xff1f;小结一、引言在前面的C#基础知识系列中介绍了从C#1.0——C#4.0中一些主要特性&#xff0c;然而.NET 4.5更新…

[libuv] libuv安装

环境: centos7.2, gcc,g version: 4.8.5 yum install -y libuv libuv-devel

pc浏览器内核详解

浏览器最重要的部分是浏览器的内核。浏览器内核是浏览器的核心&#xff0c;也称“渲染引擎”&#xff0c;用来解释网页语法并渲染到网页上。浏览器内核决定了浏览器该如何显示网页内容以及页面的格式信息。不同的浏览器内核对网页的语法解释也不同&#xff0c;因此网页开发者需…

iOS 10 的一个重要更新-自定义的通知界面

续上篇&#xff0c;在简单闹钟的例子上&#xff0c;在通知界面上显示图片动画&#xff0c;并用通知关联的按钮更新通知界面。介绍 iOS 10 通知 API 的扩展&#xff1a;自定义通知显示界面。 新框架可以统一处理本地通知和远程推送&#xff0c;同时增加了一些新 API 来控制等待中…

[USACO 1.3.3]Calf Flac

o(︶︿︶)o 烦躁&#xff0c;看了半天没看懂这个O(n)的回文串算法是什么东西&#xff0c;直接套上模板就交了。然后AC了 题目&#xff1a; Description 据说如果你给无限只母牛和无限台巨型便携式电脑(有非常大的键盘),那么母牛们会制造出世上最棒的回文。你的工作就是去这些牛…

[libuv] libuv学习

From: https://www.mobibrw.com/2016/3490 libuv 是重写了下libev&#xff0c;封装了windows和unix的差异性。 libuv的特点 非阻塞TCP套接字 socket&#xff1f; 非阻塞命名管道 UDP 定时器 子进程 fork? 通过 uv_getaddrinfo实现异步DNS 异步文件系统API uv_fs_* 高分辨率时…

elemnt的Table 表格使用注意事项

elemnt的Table 表格使用注意事项 1、修改数据 <template slot-scope"scope"><el-button size"mini" clickupdatePassword(scope.row)>修改密码</el-button><el-button size"mini" clickresetPassword(scope.row)>重置密…

px、em、rem、vm、vw 、vh、vmin 、vmax区分

Px表示“绝对尺寸”&#xff08;并非真正的绝对&#xff09;&#xff0c;实际上就是css中定义的像素&#xff08;此像素与设备的物理像素有一定的区别&#xff0c;后续详细说明见文末说明1&#xff09;&#xff0c;利用px设置字体大小及元素宽高等比较稳定和精确。Px的缺点是其…

Class.forName()用法详解

Class.forName()用法详解 标签&#xff1a; classjvmjdbc数据库documentationjava2012-03-29 09:39 40414人阅读 评论(8) 收藏 举报分类&#xff1a;Java考古学&#xff08;74&#xff09; 主要功能 Class.forName(xxx.xx.xx)返回的是一个类 Class.forName(xxx.xx.xx)的作用是要…

[poco] 访问数据库

From: https://blog.csdn.net/hl2015222050145/article/details/52335422?utm_sourceblogxgwz6 poco访问数据基本步骤&#xff1a; a. 创建会话(session) b. 从DB中读写数据 c. 使用statements d. 使用容器&#xff08;Collection&#xff09; (数据&#xff0c;集合...) …