题目相关
题目链接
AtCoder Regular Contest 106 A 题,https://atcoder.jp/contests/arc106/tasks/arc106_a。
Problem Statement
Given is an integer N. Determine whether there is a pair of positive integers (A,B) such that , and find one such pair if it exists.
Input
Input is given from Standard Input in the following format:
N
Output
If there is no pair (A,B) that satisfies the condition, print -1
.
If there is such a pair, print A and B of one such pair with space in between. If there are multiple such pairs, any of them will be accepted.
Samples1
Sample Input 1
106
Sample Output 1
4 2
Explaination
We have , so (A,B)=(4,2) satisfies the condition.
Samples2
Sample Input 2
1024
Sample Output 2
-1
Samples3
Sample Input 3
10460353208
Sample Output 3
21 1
Constraints
- All values in input are integers.
题解报告
题目翻译
给一个正数 N,问是否存在正数数对 (A, B),满足 。如果满足输出 A 和 B,如果不满足输出 -1。
题目分析
是不是 AGC 肝多了,看到这个题目,第一反应竟然是数论的题目。然后回顾我可怜的数论知识,半天都没想到方法。后面恼怒了,直接暴力枚举解决。
思路就是枚举所有 和 ,验证他们的和是否为 N。
代码细节
本题难度不大,代码细节不多。
1、数据最大为 10^18,超过了 int 的表示范围。相关的变量都需要使用 long long;
2、注意题目强调的 A 和 B 都是正数,因此 A>0 同时 B>0。
AC 参考代码
//https://atcoder.jp/contests/arc106/tasks/arc106_a
//A - 106
/*
实在想不出什么好办法,只好暴力枚举。
时间复杂度O(log_{3}{N}*log_{5}{N})
*/
#include <iostream>
using namespace std;
int main() {
long long n;
cin>>n;
for (long long i=3, ans0=1; i<=n; i*=3, ans0++) {
for (long long j=5, ans1=1; j<=n; j*=5, ans1++) {
if (i+j==n) {
cout<<ans0<<' '<<ans1<<'\n';
return 0;
}
}
}
cout<<"-1\n";
return 0;
}
复杂度
时间复杂度
时间复杂度为:。
注意循环里是 *3 和 *5,因此是对数。
空间复杂度
没什么好说的,常数级,也就是 O(1)。