UVA - 12119 The Bells are Ringing (枚举)

Perhaps you all have heard the mythical story about Tower of Hanoi (The details of this story is not required to solve this problem): “There is a tower of Hanoi with 64 disks and three pegs and the preists make one move everyday and the earth will be destroyed when all the pegs have been moved from one peg to the other following the rules of Tower of Hanoi.” In this problem we deal with a similar story – The story of an ancient temple. The ancient temple has three incredibly large bells. At the beginning of time the three bells rang together. Then the three bells never rang together and when they will ring together again the earth will be destroyed. The three bells have cycle length of t1, t2 and t3 (Here t1<t2<t3 and all are expressed in miliseconds). By this I mean that the first bell rings at every t1 seconds from the beginning, the second bell rings at every t2 second from the beginning and the third bell rings at every t3 second from the beginning. Also note that the difference of the values of t1, t2 and t3 is not that much different so that ordinary people think many time that they are ringing together.

 

Given the time difference between destruction of earth and beginning of time you will have to find the values of t1, t2 and t3.

 

Input

The input file contains at most 600 lines of inputs. Each line contains an integer which denotes (in millisecond) the time difference between the beginning of time and the time of the bells ringing together. Input is terminated by a line containing a single zero. All the input numbers will fit in a 64 bit signed integer.

 

Output
For each line of input produce two lines or more of output. The first line contains the serial of output. Each of the next lines contains three integers which denote the values of t1, t2 and t3 respectively. The value of t1, t2 and t3 is such that t1<t2<t3 and 0<t1, t2, t3≤1000000 and |t1-t3|≤25. If you cannot find values of t1, t2, twith such constraints then print the line “Such bells don’t exist” instead. In case there is more than one solution sort the output in ascending order of the value of t1, then (in case of a tie) in the ascending order of the value of t2 and then (still a tie) in ascending order of the value t3. Print a blank line after the output for each test case. Look at the output for sample input for details.

Sample Input                           Output for Sample Input

10 
103 
0 
                      

Scenario 1:

1 2 5

1 2 10

1 5 10

2 5 10

 

Scenario 2:

Such bells don't exist

 

 

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 typedef long long ll;
 6 using namespace std;
 7 const int maxn = 1000000;
 8 
 9 ll gcd(ll a, ll b) {
10     return b==0 ? a : gcd(b, a%b);    
11 }
12 
13 int main() {
14     ll lcm;
15     int cas = 1;
16     while (scanf("%lld", &lcm) != EOF && lcm) {
17         printf("Scenario %d:\n", cas++);
18         int flag = 0;
19         for (ll i = 1; i <= maxn && i <= lcm; i++) {
20             if (lcm % i)
21                 continue;
22             for (ll j = i+1; j-i <= 25; j++) {
23                 if (lcm % j)
24                     continue;
25                 ll tmp = (i * j) / gcd(i, j);
26                 for (ll k = j+1; k-i <= 25 && k <= maxn; k++) {
27                     if (lcm % k)
28                         continue;
29                     ll ans = (tmp * k) / gcd(tmp, k);
30                     if (ans == lcm) {
31                         printf("%lld %lld %lld\n", i, j, k);
32                         flag = 1;
33                     }
34                 }
35             }
36         }
37         if (!flag) 
38             printf("Such bells don't exist\n");
39         printf("\n");
40     }
41     return 0;
42 }
View Code

 

转载于:https://www.cnblogs.com/demodemo/p/4678416.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值