UVA10190 Divide, But Not Quite Conquer!等差数列
Posted 海岛Blog
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UVA10190 Divide, But Not Quite Conquer!等差数列相关的知识,希望对你有一定的参考价值。
Your goal in this problem is to divide a certain integer n by another integer m until n = 1, obtaining a sequence of numbers. Lets call a[i] each number of this sequence, and let’s say it has k numbers (i.e. you must do k − 1 succesive divisions to reach n = 1). You can only have this sequence if the following restrictions are met:
• a[1] = n, a[i] = a[i − 1] ÷ m, for all 1 < i ≤ k
• a[i] is divisible by m (that is, a[i] mod m = 0) for all 1 ≤ i < k
• a[1] > a[2] > a[3] > . . . > a[k]
For instance, if n = 125 and m = 5, you have 125, 25, 5 and 1 (you did 3 divisions: 125/5, 25/5 and 5/5). So, k = 4, a[1] = 125, a[2] = 25, a[3] = 5 and a[4] = 1.
If n = 30 and m = 3, you have 30, 10, 3 and 1. But a[2] = 10, and 10 mod 3 = 1, so there is no sequence because it violates restriction 2. When the sequence doesn’t exist we think it’s not fun and, thus, very boring!
Input
The input will consist on an arbitrary number of lines. Each line will consist of two non-negative integers n, m which are both less than 2000000000. You must read until you reach the end of file.
Output
For each pair n, m you must print the correspondent sequence a (as defined above) in a single line, with each adjacent numbers of the sequence separated by a single space. In the case the sequence doesn’t exist because it violates some restriction, just print the phrase ‘Boring!’ in a single line (without the quotes).
Sample Input
125 5
30 3
80 2
81 3
Sample Output
125 25 5 1
Boring!
Boring!
81 27 9 3 1
问题链接:UVA10190 Divide, But Not Quite Conquer!
问题简述:给定数列的首项和公比的倒数,问该数列的尾项是否为1,如果是则输出数列,否则输出“Boring!”
问题分析:数列问题,不解释。
程序说明:(略)
参考链接:(略)
题记:(略)
AC的C++语言程序如下:
/* UVA10190 Divide, But Not Quite Conquer! */
#include <bits/stdc++.h>
using namespace std;
int a[100];
int main()
{
int n, m;
while (~scanf("%d%d", &n, &m)) {
if (m < 2 || n < m) {
printf("Boring!\\n");
} else {
int cnt = 0;
while (n % m == 0 && n >= m)
a[cnt++] = n, n /= m;
a[cnt] = n;
if (a[cnt] != 1 || cnt == 0)
printf("Boring!\\n");
else {
for (int i = 0; i < cnt; i++)
printf("%d ", a[i]);
printf("%d\\n", a[cnt]);
}
}
}
return 0;
}
TLE的C++语言程序如下:
/* UVA10190 Divide, But Not Quite Conquer! */
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n, m;
while (~scanf("%d%d", &n, &m)) {
int a = 1;
while (a < n) a *= m;
if (a != n)
printf("Boring!\\n");
else {
while (n > 1) {
printf("%d ", n);
n /= m;
}
printf("%d\\n", n);
}
}
return 0;
}
以上是关于UVA10190 Divide, But Not Quite Conquer!等差数列的主要内容,如果未能解决你的问题,请参考以下文章