Math Problem
E-Math Problem
## 题面翻译
**【题目描述】**
给定两个正整数
- 选择一个整数
- 将
给定正整数
**【输入格式】**
有多组测试数据。第一行输入一个整数
第一行输入五个正整数
**【输出格式】**
每组数据输出一行一个整数,代表将
**【样例解释】**
对于第一组样例数据,一开始
- 首先进行一次第二种操作,将
- 接下来进行一次第一种操作,选择
- 接下来进行一次第一种操作,选择
- 此时
对于第二组样例数据,进行两次第二种操作将
对于第三组样例数据,因为
## 题目描述
Given two positive integers
- Choose an integer
- Change
Given a positive integer
## 输入格式
There are multiple test cases. The first line of the input contains an integer
The first line contains five integers
## 输出格式
For each test case output one line containing one integer, indicating the minimum number of coins needed to change
## 样例 #1
### 样例输入 #1
```
4
101 4 207 3 5
8 3 16 100 1
114 514 19 19 810
1 1 3 1 1
```
### 样例输出 #1
```
11
2
0
-1
```
## 提示
For the first sample test case, initially
- Firstly, perform the second type of operation once. Change
- Then, perform the first type of operation once. Choose
- Then, perform the first type of operation once. Choose
- As
For the second sample test case, perform the second type of operation twice will change
For the third sample test case, as
很显然要先除,然后再乘,枚举除的次数,一直到0为止
设一个l,r表示区间,如果说当前的值在这个区间内得到的除数都相同,那就可以继续乘,否则结束,得到结果
#include <bits/stdc++.h> #define int long long using namespace std; const int N=1e6+10,mod=1e9+7; string s; int n,t,a,b,res,ans,m,k; bool vis[N]; void solve() { cin>>n>>k>>m>>a>>b; if(k==1){ if(n%m) cout<<-1<<endl; else cout<<0<<endl; return; } res=2e18; for(int i=0;~i;n/=k,i++){ if(n==0){ res=min(res,i*b); break; } __int128_t l=n,r=n; int ans=i*b; while(l/m==r/m&&l%m){ l*=k,r=r*k+k-1; ans+=a; } res=min(res,ans); } cout<<res<<endl; return; } signed main() { std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0); cin>>t; while(t--){ solve(); } return 0; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 因为Apifox不支持离线,我果断选择了Apipost!
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端