There is an infinite set generated as follows:

  • 11 is in this set.
  • If xx is in this set, x⋅ax⋅a and x+bx+b both are in this set.

For example, when a=3a=3 and b=6b=6, the five smallest elements of the set are:

  • 11,
  • 33 (11 is in this set, so 1⋅a=31⋅a=3 is in this set),
  • 77 (11 is in this set, so 1+b=71+b=7 is in this set),
  • 99 (33 is in this set, so 3⋅a=93⋅a=9 is in this set),
  • 1313 (77 is in this set, so 7+b=137+b=13 is in this set).

Given positive integers aa, bb, nn, determine if nn is in this set.

Input

The input consists of multiple test cases. The first line contains an integer tt (1≤t≤1051≤t≤105) — the number of test cases. The description of the test cases follows.

The only line describing each test case contains three integers nn, aa, bb (1≤n,a,b≤1091≤n,a,b≤109) separated by a single space.

Output

For each test case, print "Yes" if nn is in this set, and "No" otherwise. You can print each letter in any case.

Input:

5
24 3 5
10 3 6
2345 1 4
19260817 394 485
19260817 233 264

Out:

Yes
No
Yes
No
Yes

题意:原始集合中有元素 1 ,现有数字n , a , b。我们用集合中的元素乘以a,或加b 的方式构造数字,问能不能构造出n

思路:

  • 若a == 1时:集合中元素乘以a是不变的,因此这时只能用原来集合中的1,加上若干个b构造,如果n - 1 % b == 0,则可以构造,!= 0则相反。
  • 若a > 1:设当前乘完若干个a加完若干个b后数字是x(x = ((x + kb)*a + b)),看现在的x加上若干个b能不能构造出n,因此只要此时的n -  x % b == 0即可构造成功,不能的话,就要乘以a。x进入下一轮循环(加上若干b,再乘以a)。

关键:虽说可以任意顺序加b或者乘a,但事实上加若干b,有时是加0个b,所以就是*a + kb的顺序进行下去的 ,因此只需要判定n-x % b == 0即可,那为什么下列代码中是直接减去pow(a,j)再判断是否%b==0呢?

再看(x*a + k*b)*a = x*a^2 + k*a*b 注意到,后面省去的k*a*b,一定是可以由若干个b构成的,当

(n - x*a^2 )% b的时候还可以用若干k*a*b + k1*b来补齐剩余。

AC代码:

#include <bits/stdc++.h>
#define int long long
using namespace std;const int N = 1e5 + 10;int a[N];void solve(){int n,a,b; cin >> n >> a >> b;if(a == 1){if((n-1)%b==0)cout << "YES\n";else cout << "NO\n";}else{for(int j = 1;j <= n;j*=a){if((n - j) % b == 0){cout << "YES\n";return;}}cout << "NO\n";}
}signed main(){int t; cin >> t;while(t--){solve();} return 0;
}

Plus and Multiply相关推荐

  1. tf.matmul / tf.multiply

    import tensorflow as tf import numpy as np 1.tf.placeholder placeholder()函数是在神经网络构建graph的时候在模型中的占位,此 ...

  2. tf.matmul() 和tf.multiply() 的区别

    1.tf.multiply()两个矩阵中对应元素各自相乘 格式: tf.multiply(x, y, name=None)  参数:  x: 一个类型为:half, float32, float64, ...

  3. python中np.multiply()、np.dot()和星号(*)三种乘法运算的区别

    1. np.dot() 对类型为 ndarray 的数据: 一维情况:为点乘,即对应元素相乘再相加 二维情况:矩阵乘法 对类型为 matrix 的数据:矩阵乘法 一个二维数组跟一个大小合适的一维数组的 ...

  4. [leetcode]Multiply Strings @ Python

    原题地址:https://oj.leetcode.com/problems/multiply-strings/ 题意: Given two numbers represented as strings ...

  5. latex Label ' ' multiply defined

    出现这个问题是label多次定义了,比如 Label ' test ' multiply defined 也就是说latex中有至少两个地方将label都标注为test了,这个时候找到这样的label ...

  6. 细分tf.multiply()、tf.matmul()、tf.scalar_mul()函数

    tf.multiply() 释义:将两个矩阵中对应元素各自相乘 示例: import tensorflow as tfX = tf.constant([[1, 2, 3], [4, 5 ,6]], d ...

  7. LeetCode Multiply Strings(大整数相乘)

    思路:用笔算的形式就可以了 代码如下: public class Solution {public String multiply(String num1, String num2) {StringB ...

  8. leetcode 43. 字符串相乘(Multiply Strings)

    目录 题目描述: 示例 1: 示例 2: 解法: 题目描述: 给定两个以字符串形式表示的非负整数 num1 和 num2,返回 num1 和 num2 的乘积,它们的乘积也表示为字符串形式. 示例 1 ...

  9. pandas.Series.multiply()含义解释

    简述 可以和下面这个篇文章对照着看,有多不同 pandas.DataFrame.multiply()含义解释 这个非常有意思,这个也是类似于乘法,但是是对应位置相乘的乘法 >>> i ...

  10. pandas.DataFrame.multiply()含义解释

    简述 可以和下面这篇文章对比着看,有所不同 pandas.Series.multiply()含义解释 其实,就是做乘法. 比如: >>> pd.DataFrame({'A':[1, ...

最新文章

  1. SpringMVC上传文件以流方式判断类型附常用类型
  2. 既然是青春,怎么能错过?唯一理由只缺1张免费票!
  3. C语言中const的用法
  4. sklearn自学指南(part32)--保序回归
  5. 剑指offer-数组中的重复的数字-p39
  6. 字符串固定长度 易语言_易语言字符串操作源码
  7. java 图片特效_强大的Java图像滤镜特效类库Java Image Filters
  8. Mysql 主从复制简易操作指南
  9. 汉王考勤系统服务器IP,汉王人脸识别考勤客户端使用说明
  10. 苹果电脑修改MAC地址方法
  11. 减速机的漏油原因及快速维修方法
  12. 【微信video视频播放】video标签
  13. VScode 常用插件推荐
  14. 王道数据结构2.2.4——1、设计递归算法删除不带头结点的单链表L中所有值为x的结点
  15. Top 11 Best Practices for PHP Development
  16. 怎么修改打印机服务器权限,打印机管理_怎样设置打印机管理权限
  17. 珠宝erp是否能带回珠宝行业的“黄金时代”?
  18. 拒绝白嫖,著名开源项目作者暴力删库,导致账号被封!
  19. 互联网摸鱼日报(2022-11-22)
  20. 吴恩达深度学习资料 Quiz+ 编程+ ppt+学习笔记(可jupyter笔记本实战)

热门文章

  1. CAS算法-实现原理
  2. iOS自动化部署方案Jenkins Fastlane code.aliyun 蒲公英 appStore
  3. 计算机管理恢复分区,Win10硬盘新增的恢复分区是什么?
  4. android手机时钟、闹钟、计时器、秒表app源码
  5. Date 类 getTime() 方法
  6. Sigil制作epub,正则表达式的使用
  7. strstr函数.c
  8. 如何看待国企纷纷卸载微软Office改用金山WPS?
  9. ufo帧率测试网站_手机相机拍摄的视频帧率的测试方法
  10. Python 编程导论 Chapter 4 —— 函数、作用域与抽象