Plus and Multiply(1500)

2024-08-29 10:52
文章标签 plus 1500 multiply

本文主要是介绍Plus and Multiply(1500),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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

这篇关于Plus and Multiply(1500)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1117658

相关文章

MyBatis Plus大数据量查询慢原因分析及解决

《MyBatisPlus大数据量查询慢原因分析及解决》大数据量查询慢常因全表扫描、分页不当、索引缺失、内存占用高及ORM开销,优化措施包括分页查询、流式读取、SQL优化、批处理、多数据源、结果集二次... 目录大数据量查询慢的常见原因优化方案高级方案配置调优监控与诊断总结大数据量查询慢的常见原因MyBAT

MyBatis/MyBatis-Plus同事务循环调用存储过程获取主键重复问题分析及解决

《MyBatis/MyBatis-Plus同事务循环调用存储过程获取主键重复问题分析及解决》MyBatis默认开启一级缓存,同一事务中循环调用查询方法时会重复使用缓存数据,导致获取的序列主键值均为1,... 目录问题原因解决办法如果是存储过程总结问题myBATis有如下代码获取序列作为主键IdMappe

MyBatis Plus实现时间字段自动填充的完整方案

《MyBatisPlus实现时间字段自动填充的完整方案》在日常开发中,我们经常需要记录数据的创建时间和更新时间,传统的做法是在每次插入或更新操作时手动设置这些时间字段,这种方式不仅繁琐,还容易遗漏,... 目录前言解决目标技术栈实现步骤1. 实体类注解配置2. 创建元数据处理器3. 服务层代码优化填充机制详

mybatis-plus如何根据任意字段saveOrUpdateBatch

《mybatis-plus如何根据任意字段saveOrUpdateBatch》MyBatisPlussaveOrUpdateBatch默认按主键判断操作类型,若需按其他唯一字段(如agentId、pe... 目录使用场景方法源码方法改造首先在service层定义接口service层接口实现总结使用场景my

MyBatis-plus处理存储json数据过程

《MyBatis-plus处理存储json数据过程》文章介绍MyBatis-Plus3.4.21处理对象与集合的差异:对象可用内置Handler配合autoResultMap,集合需自定义处理器继承F... 目录1、如果是对象2、如果需要转换的是List集合总结对象和集合分两种情况处理,目前我用的MP的版本

MyBatis-Plus 与 Spring Boot 集成原理实战示例

《MyBatis-Plus与SpringBoot集成原理实战示例》MyBatis-Plus通过自动配置与核心组件集成SpringBoot实现零配置,提供分页、逻辑删除等插件化功能,增强MyBa... 目录 一、MyBATis-Plus 简介 二、集成方式(Spring Boot)1. 引入依赖 三、核心机制

Mybatis-Plus 3.5.12 分页拦截器消失的问题及快速解决方法

《Mybatis-Plus3.5.12分页拦截器消失的问题及快速解决方法》作为Java开发者,我们都爱用Mybatis-Plus简化CRUD操作,尤其是它的分页功能,几行代码就能搞定复杂的分页查询... 目录一、问题场景:分页拦截器突然 “失踪”二、问题根源:依赖拆分惹的祸三、解决办法:添加扩展依赖四、分页

MyBatis-Plus 自动赋值实体字段最佳实践指南

《MyBatis-Plus自动赋值实体字段最佳实践指南》MyBatis-Plus通过@TableField注解与填充策略,实现时间戳、用户信息、逻辑删除等字段的自动填充,减少手动赋值,提升开发效率与... 目录1. MyBATis-Plus 自动赋值概述1.1 适用场景1.2 自动填充的原理1.3 填充策略

mybatis-plus QueryWrapper中or,and的使用及说明

《mybatis-plusQueryWrapper中or,and的使用及说明》使用MyBatisPlusQueryWrapper时,因同时添加角色权限固定条件和多字段模糊查询导致数据异常展示,排查发... 目录QueryWrapper中or,and使用列表中还要同时模糊查询多个字段经过排查这就导致只要whe

MyBatis-Plus通用中等、大量数据分批查询和处理方法

《MyBatis-Plus通用中等、大量数据分批查询和处理方法》文章介绍MyBatis-Plus分页查询处理,通过函数式接口与Lambda表达式实现通用逻辑,方法抽象但功能强大,建议扩展分批处理及流式... 目录函数式接口获取分页数据接口数据处理接口通用逻辑工具类使用方法简单查询自定义查询方法总结函数式接口