close
Content

此問題的目標是將某個整數n除以另一個整數m直到 n = 1,這方法將會獲得一個數字序列。
我們假設該序列的每個數字為a[i],假設它有k個數字(即必須進行 k−1 個連續除法才能達到 n = 1)。

根據以下限制,此序列必唯一:
1. a[1] = n, a[i] = a[i − 1] ÷ m, for all 1 < i ≤ k
2. a[i] 被 m 整除(a[i] mod m = 0) for all 1 ≤ i < k
3. a[1] > a[2] > a[3] > ... > a[k]

以下為舉例:
如果n = 125且m = 5,則根據上述過程會得到125、25、5、1(做了3次除法:125/5、25/5、5/5)。
因此,k = 4,a[1] = 125、a[2] = 25、a[3] = 5、a[4] = 1。
如果n = 30且m = 3,則根據上述過程會得到30、10、3、1。
但是a[2] = 10 且 10 mod 3 = 1,違反了限制2,所以此序列不存在。
如果序列不存在,我們認為這不好玩,因此非常"Boring!"

Input

輸入包含多行。
每行有兩個非負整數n和m,n和m皆小於2000000000。

Output

對於每行,輸出序列a(如題目定義),序列的每個相鄰數字之間用一個空格隔開。
如果序列不存在,則輸出"Boring!"

Sample Input #1
125 5
30 3
80 2
81 3
Sample Output #1
125 25 5 1
Boring!
Boring!
81 27 9 3 1

python:

from sys import stdin
for f in stdin:
    n,m=map(int,f.split())
    arr=[n]
    s=0 #S作為輸出的機關
    w=0
    if(m==0 or m==1):
        print('Boring!')
        w+=1
    if(w==0):
        while (n%m==0):
            arr.append(n//m)
            if(n//m==1):break
            n=n//m
        if(n%m!=0):
            s+=1
            print('Boring!')
        if(s==0):
            print(*arr)   
arrow
arrow
    創作者介紹
    創作者 趴趴熊日常 的頭像
    趴趴熊日常

    資工趴趴熊的小天地

    趴趴熊日常 發表在 痞客邦 留言(0) 人氣()