Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> POJ 1365 Prime Land

POJ 1365 Prime Land

編輯:關於Android編程

Prime Land Time Limit: 1000MS
Memory Limit: 10000K Total Submissions: 2972
Accepted: 1362

Description

Everybody in the Prime Land is using a prime base number system. In this system, each positive integer x is represented as follows: Let {pi}i=0,1,2,... denote the increasing sequence of all prime numbers. We know that x > 1 can be represented in only one way in the form of product of powers of prime factors. This implies that there is an integer kx and uniquely determined integers ekx, ekx-1, ..., e1, e0, (ekx > 0), that \ The sequence

(ekx, ekx-1<喎?/kf/ware/vc/" target="_blank" class="keylink">vc3ViPiwgLi4uICxlPHN1Yj4xPC9zdWI+LCBlPHN1Yj4wPC9zdWI+KSA8YnI+Cjxicj4KPGJyPgo8YnI+CmlzIGNvbnNpZGVyZWQgdG8gYmUgdGhlIHJlcHJlc2VudGF0aW9uIG9mIHggaW4gcHJpbWUgYmFzZSBudW1iZXIgc3lzdGVtLiA8YnI+Cjxicj4KSXQgaXMgcmVhbGx5IHRydWUgdGhhdCBhbGwgbnVtZXJpY2FsIGNhbGN1bGF0aW9ucyBpbiBwcmltZSBiYXNlIG51bWJlciBzeXN0ZW0gY2FuIHNlZW0gdG8gdXMgYSBsaXR0bGUgYml0IHVudXN1YWwsIG9yIGV2ZW4gaGFyZC4gSW4gZmFjdCwgdGhlIGNoaWxkcmVuIGluIFByaW1lIExhbmQgbGVhcm4gdG8gYWRkIHRvIHN1YnRyYWN0IG51bWJlcnMgc2V2ZXJhbCB5ZWFycy4gT24gdGhlIG90aGVyIGhhbmQsIG11bHRpcGxpY2F0aW9uIGFuZCBkaXZpc2lvbgogaXMgdmVyeSBzaW1wbGUuIDxicj4KPGJyPgpSZWNlbnRseSwgc29tZWJvZHkgaGFzIHJldHVybmVkIGZyb20gYSBob2xpZGF5IGluIHRoZSBDb21wdXRlciBMYW5kIHdoZXJlIHNtYWxsIHNtYXJ0IHRoaW5ncyBjYWxsZWQgY29tcHV0ZXJzIGhhdmUgYmVlbiB1c2VkLiBJdCBoYXMgdHVybmVkIG91dCB0aGF0IHRoZXkgY291bGQgYmUgdXNlZCB0byBtYWtlIGFkZGl0aW9uIGFuZCBzdWJ0cmFjdGlvbiBpbiBwcmltZSBiYXNlIG51bWJlciBzeXN0ZW0gbXVjaCBlYXNpZXIuIEl0IGhhcyBiZWVuIGRlY2lkZWQKIHRvIG1ha2UgYW4gZXhwZXJpbWVudCBhbmQgbGV0IGEgY29tcHV0ZXIgdG8gZG8gdGhlIG9wZXJhdGlvbiBgYG1pbnVzIG9uZQ=="'.

Help people in the Prime Land and write a corresponding program.

For practical reasons we will write here the prime base representation as a sequence of such pi and ei from the prime base representation above for which ei > 0. We will keep decreasing order with regard to pi.

Input

The input consists of lines (at least one) each of which except the last contains prime base representation of just one positive integer greater than 2 and less or equal 32767. All numbers in the line are separated by one space. The last line contains number 0.

Output

The output contains one line for each but the last line of the input. If x is a positive integer contained in a line of the input, the line in the output will contain x - 1 in prime base representation. All numbers in the line are separated by one space. There is no line in the output corresponding to the last ``null'' line of the input.

Sample Input

17 1
5 1 2 1
509 1 59 1
0

Sample Output

2 4
3 2
13 1 11 1 7 1 5 1 3 1 2 1

覺得這題最難的就是理解題意了 應該怪我英語太差了

題意 一個大於1的數可以 表示成p1^e1*p2^e2*p3^e3*p4^e4*p5^e5*...*pn^en的形式 其中p是素數 且p是遞減的 p< 32767

輸入給你這個數的這種表達形式的所有p和e 第奇數個數為pi第偶數個數為ei 每行的所有數據表示一個數 你要求出這個數x然後輸出x-1的這種表達形式;

理解了題意這個題就簡單了 直接暴力可能會超時 所以還是打了個素數表 還要注意pow的前一個參數要乘以1.0 不然會精度丟失

#include
#include
#include
using namespace std;
const int N = 33000;
int x, a, b , m , last , ans[N], vis[N], prime[N];

void initPrime()
{
    int num = 0, m = sqrt (N + 0.5);
    for (int i = 2; i <= m; ++i)
    {
        if (vis[i] == 0)
            for (int j = i * i; j < N; j += i)
                vis[j] = 1;
    }
    for (int i = 2, t = 0; i < N; ++i)
        if (!vis[i]) prime[++num] = i;
}

int main()
{
    initPrime(); char c;
    while (scanf ("%d", &a), a)
    {
        scanf ("%d%c", &b, &c);
        x = pow (a*1.0, b);
        while (c == ' ')
        {
            scanf ("%d %d%c", &a, &b, &c);
            x = x * pow (a*1.0, b);
        }
        --x; last = 0;
        memset(ans,0,sizeof(ans));

        for (int i = 1; x > 1; m=i,++i)
            if (x % prime[i] == 0)
            {
                if (!last) last = i;
                while (x % prime[i] == 0)
                {
                    ++ans[i];
                    x /= prime[i];
                }
            }
        for (int i = m; i >= last; --i)
            if (ans[i]) printf (i == last ? "%d %d\n" : "%d %d ", prime[i], ans[i]);
    }
    return 0;
}


  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved