[UVA][建表、枚] 11099 - Next Same-Factored@Morris' Blog|PChome Online 人新台
2013-05-31 18:27:42| 人799| 回0 | 上一篇 | 下一篇

[UVA][建表、枚] 11099 - Next Same-Factored

0 收藏 0 0 站台

Problem F
Next Same-Factored

Time Limit: 6 Seconds

Just print out the next integer number which has the same prime factors as the given number and is less than 2,000,000.

Input

There will be several test cases (at most 100,000). Each case is a single positive integer n that is less than 1,000,000, on a separate line.

Output

For each test case, output a line containing a single integer which is the next integer number that have the same prime factors as n. In the case that there is no such number print one line stating "Not Exist!".

Sample Input                               Output for Sample Input

2
143
991
4
1573
982081

Problem setter: Hossein Azizpour
Special Thanks to: Ali Sharifrazavian
Alternate Solution: Ali Sharifrazavian, Hadi Moshayedi

1st Amirkabir UT Annual Programming Contest  Qualification Round


目描述:
要求找到下一比 n 大的字,且 n 具有相同因的集合。

解法1:

建表,每字的因(不管次方)找出,每字的因乘起表示成一字 v,
因此有性 n, v,
例如 n = 2^3 * 5^2 * 7^5, v = 2*5*7

那排序一下,二分搜答案。

由於高 200 的排序,因此理上很慢,O(nlogn), 至少要跑 500 ms

#include <stdio.h>
#include <algorithm>
using namespace std;
#define maxL (2000000>>5)+1
#define GET(x) (mark[(x)>>5]>>((x)&31)&1)
#define SET(x) (mark[(x)>>5] |= 1<<((x)&31))
int mark[maxL];
struct E {
    int n, pi;
    bool operator<(const E &a) const {
        if(pi != a.pi)
            return pi < a.pi;
        return n < a.n;
    }
};
E D[2000005];
int S[2000005];
void sieve() {
    register int i, j, k;
    SET(1);
nbsp;   int n = 2000000;
    for(i = 2; i <= n; i++)
        D[i].n = i, D[i].pi = 1;
    for(i = 2; i <= n; i++) {
        if(!GET(i)) {
            D[i].pi = i;
            for(k = n/i, j = i*k; k >= 2; k--, j -= i) {
                D[j].pi *= i;
                SET(j);
            }
        }
        S[i] = D[i].pi;
    }
    D[0].pi = 0;
    D[1].pi = 1;
}
int main() {
    sieve();
    long long n;
    return 0;
    sort(D, D+2000001);
    int i;
    while(scanf("%lld", &n) == 1) {
        if(n < 2) {
            puts("Not Exist!");
            continue;
        }
        E test;
        test.n = n, test.pi = S[n];
        int pos = upper_bound(D, D+2000001, test) - D;
        if(pos <= 2000000 && D[pos].pi == test.pi)
            printf("%d\n", D[pos].n);
        else
            puts("Not Exist!");
    }
    return 0;
}


解法2:

仔想想,乾脆求出因,然後枚次方,根入,枚就得有效率,
由於具有相同因的字小於 200 ,至多只有 300 。


#include <stdio.h>
#include <algorithm>
using namespace std;
#define maxL (1000>>5)+1
#define GET(x) (mark[(x)>>5]>>((x)&31)&1)
#define SET(x) (mark[(x)>>5] |= 1<<((x)&31))
int mark[maxL];
int pm[1000], pt = 0;
void sieve() {
    register int i, j, k;
    SET(1);
    int n = 1000;
    for(i = 2; i <= n; i++) {
        if(!GET(i)) {
            pm[pt++] = i;
            for(k = n/i, j = i*k; k >= i; k--, j -= i)
                SET(j);
        }
    }
}
int f[1000], fidx, ret;
int n, m;
int i, j, k;
void dfs(int idx, long long mult) {
    if(idx == fidx) return;
    for(; mult <= ret; mult *= f[idx]) {
        dfs(idx+1, mult);
        if(mult > n)
            ret = min(ret, (int)mult);
    }
}
int main() {
    sieve();
    while(scanf("%d", &n) == 1) {
        if(n < 2) {
            puts("Not Exist!");
            continue;
        }
        m = n;
        fidx = 0;
        int base = 1;
        for(i = 0; i < pt && pm[i]*pm[i] <= m; i++) {
            j = pm[i];
            if(m%j == 0) {
                k = 0;
                while(m%j == 0) k++, m /= j;
                f[fidx++] = j, base *= j;
            }
        }
        if(fidx == 0) {//prime
            if(n > 1414)
                puts("Not Exist!");
            else
                printf("%d\n", n*n);
            continue;
        }
        if(m != 1)
            f[fidx++] = m, base *= m;
        ret = 2000000;
        dfs(0, base);
        if(ret < 2000000)
            printf("%d\n", ret);
        else
            puts("Not Exist!");
    }
    return 0;
}


台: Morris
人(799) | 回(0)| 推 (0)| 收藏 (0)|
全站分: 不分 | 人分: UVA |
此分下一篇:[UVA][math] 11424 - GCD - Extreme (I)
此分上一篇:[UVA][充里德] 10090 - Marbles

是 (若未登入"人新台"看不到回覆唷!)
* 入:
入片中算式的果(可能0) 
(有*必填)
TOP
全文
ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86