24h購物| | PChome| 登入
2012-10-06 19:53:03| 人氣895| 回應0 | 上一篇 | 下一篇

[UVA][Greedy] 10527 - Persistent Numbers

推薦 0 收藏 0 轉貼0 訂閱站台

Problem B: Persistent Numbers


123456789
1 1  2  3  4  5  6  7  8  9
2  2  4  6  8 1012141618
3  3  6  9 121518212427
4  4  8 12162024283236
5  5 1015202530354045
6  6 1218243036424854
7  7 1421283542495663
8  8 1624324048566472
9  9 1827364554637281
The multiplicative persistence of a number is defined by Neil Sloane (Neil J.A. Sloane in The Persistence of a Number published in Journal of Recreational Mathematics 6, 1973, pp. 97-98., 1973) as the number of steps to reach a one-digit number when repeatedly multiplying the digits. Example:
679 -> 378 -> 168 -> 48 -> 32 -> 6.
That is, the persistence of 679 is 5. The persistence of a single digit number is 0. At the time of this writing it is known that there are numbers with the persistence of 11. It is not known whether there are numbers with the persistence of 12 but it is known that if they exists then the smallest of them would have more than 3000 digits.

The problem that you are to solve here is: what is the smallest number such that the first step of computing its persistence results in the given number?

For each test case there is a single line of input containing a decimal number with up to 1000 digits. A line containing -1 follows the last test case. For each test case you are to output one line containing one integer number satisfying the condition stated above or a statement saying that there is no such number in the format shown below.

Sample input

0
1
4
7
18
49
51
768
-1

Output for sample input

10
11
14
17
29
77
There is no such number.
2688

P. Rudnicki

找一個最小的數字其每個位數相乘會等於 n, 個位數字特別討論,
接著從 9 開始試除直到 2, 如果仍然除不等於1則輸出無解

#include <stdio.h>
#include <stdlib.h>
int main() {
    char s[3005];
    int i, j;
    while(gets(s)) {
        if(s[0] == '-') break;
        if(s[1] == '\0') {
            printf("1%s\n", s);
            continue;
        }
        for(i = 0; s[i]; i++)   s[i] -= '0';
        int len = i, idx = 0, st = 0;
        char stk[3005];
        for(i = 9; i >= 2; i--) {
            while(1) {
                int mod = 0;
                for(j = st; j < len; j++) {
                    mod = mod*10 + s[j];
                    mod %= i;
                }
                if(!mod) {
                    stk[idx++] = i+'0';
                    mod = 0;
                    for(j = st; j < len; j++) {
                        mod = mod*10 + s[j];
                        s[j] = mod/i;
                        mod %= i;
                    }
                    while(s[st] == 0)   st++;
                } else  break;
            }
        }
        if(st == len-1 && s[st] == 1) {
            for(i = idx-1; i >= 0; i--)
                putchar(stk[i]);
            puts("");
        } else
            puts("There is no such number.");
    }
    return 0;
}

台長: Morris
人氣(895) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA][brute] 11218 - KTV
此分類上一篇:[UVA][樹走訪] 10562 - Undraw the Trees

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文