zoukankan      html  css  js  c++  java
  • SDNU 1427.分解质因数(水题)

    Description

    问题描述   求出区间[a,b]中所有整数的质因数分解。 输入格式   输入两个整数a,b。 输出格式   每行输出一个数的分解,形如k=a1*a2*a3...(a1< =a2< =a3...,k也是从小到大的)(具体可看样例) 样例输入 3  10 样例输出 3=3 4=2*2 5=5 6=2*3 7=7 8=2*2*2 9=3*3 10=2*5 提示   先筛出所有素数,然后再分解。 数据规模和约定   2< =a< =b< =10000

    Input

     

    Output

     

    Sample Input

     

    Sample Output

     
    思路:明明就是一道水题,debug却让我吐血!一开始我用vector来存数据,但是爆了,也不知道为什么会爆,我明明开了很大的数。后来只能用简化的办法了。
    #include <cstdio>
    #include <iostream>
    #include <cmath>
    #include <string>
    #include <cstring>
    #include <algorithm>
    #include <queue>
    #include <vector>
    #include <map>
    using namespace std;
    #define ll long long
    const int inf = 0x3f3f3f3f;
    const int mod = 1e9+7;
    
    int a, b, tot, pri[10000+8], en[10000+8];
    bool is[10000+8];
    
    void E()
    {
        tot = 0;
        memset(is, 1, sizeof(is));
        is[0] = is[1] = 0;
        for(int i = 2; i<10000+8; i++)
        {
            if(is[i])
            {
                pri[tot++] = i;
                for(int j = i+i; j<10000+8; j += i)
                    is[j] = 0;
            }
        }
    }
    
    int main()
    {
        E();
        while(~scanf("%d%d", &a, &b))
        {
            for(int i = a; i <= b; i++)
            {
                int id = 0, buffer = i;
                for(int j = 0; j < tot && pri[j]*pri[j] <= i; j++)
                {
                    if(buffer%pri[j] == 0)
                    {
                        while(buffer%pri[j] == 0)
                        {
                            en[id++] = pri[j];
                            buffer /= pri[j];
                        }
                    }
                }
                if(buffer>1)en[id++] = buffer;
                printf("%d=", i);
                bool flag = 0;
                for(int j = 0; j<id; j++)
                {
                    if(flag)printf("*");
                    flag = 1;
                    printf("%d", en[j]);
                }
                printf("
    ");
            }
        }
        return 0;
    }
  • 相关阅读:
    Codeforces Round #307 (Div. 2)E. GukiZ and GukiZiana
    bzoj2957: 楼房重建,分块
    分块入门。
    poj3690 Constellations
    Codeforces Round #451 (Div. 2)F. Restoring the Expression 字符串hash
    Codeforces Round #456 (Div. 2)D. Fishes
    Codeforces Round #450 (Div. 2)D. Unusual Sequences
    快速排序+分治法
    哈夫曼编码课程设计+最小优先对列建树。
    浅谈差分约束系统
  • 原文地址:https://www.cnblogs.com/RootVount/p/11252860.html
Copyright © 2011-2022 走看看