Plus and Multiply(1500)

embedded/2024/10/18 18:13:42/

Plus and Multiply

题面翻译

有一个无穷大的正整数集合 S S S,该集合按下面所述方法生成:

  • 数字 1 1 1 在集合 S S S 中。

  • 若数字 x x x 在该集合中,那么数 x × a x \times a x×a 和数 x + b x+b x+b 均在集合 S S S 中。(其中 a a a b b b 为给定常数)

现在给出数 n , a , b n,a,b n,a,b,请判断 n n n 是否在集合 S S S 中(此处给出的 a a a b b b 就是上述集合生成方法中的 a a a b b b),若在请输出 Yes,否则输出 No。多组数据。

令数据组数为 t t t,那么有 1 ≤ t ≤ 1 0 5 1 \leq t \leq 10^5 1t105 1 ≤ n , a , b ≤ 1 0 9 1 \leq n,a,b \leq 10^9 1n,a,b109

题目描述

There is an infinite set generated as follows:

  • $ 1 $ is in this set.
  • If $ x $ is in this set, $ x \cdot a $ and $ x+b $ both are in this set.

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

  • $ 1 $ ,
  • $ 3 $ ( $ 1 $ is in this set, so $ 1\cdot a=3 $ is in this set),
  • $ 7 $ ( $ 1 $ is in this set, so $ 1+b=7 $ is in this set),
  • $ 9 $ ( $ 3 $ is in this set, so $ 3\cdot a=9 $ is in this set),
  • $ 13 $ ( $ 7 $ is in this set, so $ 7+b=13 $ is in this set).

Given positive integers $ a $ , $ b $ , $ n $ , determine if $ n $ is in this set.

输入格式

The input consists of multiple test cases. The first line contains an integer $ t $ ( $ 1\leq t\leq 10^5 $ ) — the number of test cases. The description of the test cases follows.

The only line describing each test case contains three integers $ n $ , $ a $ , $ b $ ( $ 1\leq n,a,b\leq 10^9 $ ) separated by a single space.

输出格式

For each test case, print “Yes” if $ n $ is in this set, and “No” otherwise. You can print each letter in any case.

样例 #1

样例输入 #1

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

样例输出 #1

Yes
No
Yes
No
Yes

提示

In the first test case, $ 24 $ is generated as follows:

  • $ 1 $ is in this set, so $ 3 $ and $ 6 $ are in this set;
  • $ 3 $ is in this set, so $ 9 $ and $ 8 $ are in this set;
  • $ 8 $ is in this set, so $ 24 $ and $ 13 $ are in this set.

Thus we can see $ 24 $ is in this set.

The five smallest elements of the set in the second test case is described in statements. We can see that $ 10 $ isn’t among them.

思路:因为只有乘法和加法假设数为x,则我们先乘以a然后在加上b,与我们先加上b在乘以a其实是等价的,因为变换次数是无限的,因此我们可以想到n = a的x次方 + b的y次方,因为指数型函数指数爆炸式增长,因此我们来枚举x,枚举过程中我们只需要判断n - a的x次方对b取模是不是0即可

AC代码:

#include<bits/stdc++.h>using namespace std;typedef long long ll;
typedef pair<int, int>PII;
typedef pair<int, double>PDD;
const int N=2e5 +10;
const int MOD = 1e9 + 7;
const int INF=0X3F3F33F;
const int dx[]={-1,0,1,0,-1,-1,+1,+1};
const int dy[]={0,1,0,-1,-1,+1,-1,+1}; //马
const int dxx[]={-1,2,1,1,-1,2,-2,-2};
const int dyy[]={2,1,-2,2,-2,-1,-1,1};    
const int M = 1e7 + 10;int t;
int main()
{cin >> t;while(t --){ll n, a, b;cin >> n >> a >> b;if(a == 1){if((n - 1) % b == 0) puts("Yes");else puts("No");}else {int f = 0;for(ll i = 1; i <= n; i *= a){if((n - i) % b == 0){f = 1;break;}}if(f) puts("Yes");else puts("No");}}return 0;
}

http://www.ppmy.cn/embedded/103139.html

相关文章

【Python机器学习】NLP分词——利用分词器构建词汇表(五)——将词汇表扩展到n-gram

目录 n-gram概念 停用词 n-gram概念 n-gram是一个最多包含n个元素的序列&#xff0c;这些元素从由它们组成的序列&#xff08;通常是字符串&#xff09;中提取而成。一般来说&#xff0c;n-gram的“元素”可以使字符、音节、词&#xff0c;甚至是像A、T、G、C等表示DNA序列的…

R 语言学习教程,从入门到精通,R 绘图 散点图(25)

1、R 绘图 散点图 散点图是将所有的数据以点的形式展现在直角坐标系上&#xff0c;以显示变量之间的相互影响程度&#xff0c;点的位置由变量的数值决定&#xff0c;每个点对应一个 X 和 Y 轴点坐标。 散点图可以使用 plot() 函数来绘制&#xff0c;语法格式如下&#xff1a; …

【Rust光年纪】深度解读:Rust语言中各类消息队列客户端库详细对比

选择最佳 Rust 消息队列客户端库&#xff1a;全面对比与分析 前言 随着现代应用程序的复杂性不断增加&#xff0c;消息队列成为构建可靠、高性能系统的重要组件。本文将介绍一些用于Rust语言的消息队列客户端库&#xff0c;包括AMQP、Apache Kafka、NSQ、Apache Pulsar和Rock…

SpringMVC 笔记篇

1.1 执行流程 1.1.7 RequestMappingHandlerAdapter.afterPropertiesSet() 除了HandlerMapping之外&#xff0c;其他的有没有自己的初始化逻辑呢&#xff0c;也是有的&#xff0c;我们拿HandlerAdapter中的RequestMappingHandlerAdapter来举例&#xff0c;那么首先我们找到Requ…

代码随想录Day 28|题目:122.买卖股票的最佳时机Ⅱ、55.跳跃游戏、45.跳跃游戏Ⅱ、1005.K次取反后最大化的数组和

提示&#xff1a;DDU&#xff0c;供自己复习使用。欢迎大家前来讨论~ 文章目录 题目题目一&#xff1a;122.买卖股票的最佳时机 II贪心算法&#xff1a;动态规划 题目二&#xff1a;55.跳跃游戏解题思路&#xff1a; 题目三&#xff1a; 45.跳跃游戏 II解题思路方法一方法二 题…

3160. 所有球里面不同颜色的数目(java)

3160. 所有球里面不同颜色的数目(java) Java hashmap的merge方法&#xff1a; 1.merge是直接运行修改的&#xff0c;比如 if(colors.merge(curc,-1,Integer::sum)0) &#xff0c;不论结果真假&#xff0c;这个curc对应的值已经减一了。 2.字典的size和remove方法&#xff1b; …

设计模式之工厂模式和策略模式的区别

介绍 工厂模式(Factory Pattern) 和 策略模式(Strategy Pattern) 是两种常见的设计模式,在软件开发中有着不同的用途和实现方式。 工厂模式(Factory Pattern) 工厂模式是一种创建型设计模式,它提供了一种创建对象的方式,而无需在代码中显式指定要创建的具体类。它将…

Jiujiu-SaaS:开创Web3时代的IP电商新纪元

在数字经济的迅猛发展下&#xff0c;传统电商平台面临着日益复杂的市场竞争和多样化的用户需求。为应对这一挑战&#xff0c;Jiujiu-SaaS作为一款开源的Web3 IP电商平台&#xff0c;正以其去中心化、高透明度和社区用户主导的特性&#xff0c;引领着电商领域的革新浪潮。 一、…