首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >POJ 刷题系列:2739. Sum of Consecutive Prime Numbers

POJ 刷题系列:2739. Sum of Consecutive Prime Numbers

作者头像
用户1147447
发布于 2018-01-02 02:01:47
发布于 2018-01-02 02:01:47
65400
代码可运行
举报
文章被收录于专栏:机器学习入门机器学习入门
运行总次数:0
代码可运行

POJ 刷题系列:2739. Sum of Consecutive Prime Numbers

传送门:POJ 2739. Sum of Consecutive Prime Numbers

题意:

给定一个数num,求连续的素数和等于num的个数。

思路: 题目给定了连续素数,所以只需要从小于num的素数开始不断累加,知道sum >= num,其中若sum == num则计数。素数打表采用艾氏筛选法。

代码如下:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Map;
import java.util.StringTokenizer;

public class Main{

    String INPUT = "./data/judge/201712/P2739.txt";

    public static void main(String[] args) throws IOException {
        new Main().run();
    }

    static final int MAX_N = 10000 + 16;

    int[] prime = new int[MAX_N];
    boolean[] isPrime = new boolean[MAX_N];
    int tot;

    void sieve() {
        Arrays.fill(isPrime, true);
        for (int i = 2; i < MAX_N; ++i) {
            if (isPrime[i]) {
                prime[tot++] = i;
                for (int j = 2 * i; j < MAX_N; j += i) {
                    isPrime[j] = false;
                }
            }
        }
    }


    int solve(int num) {
        int count = 0;
        for (int i = 0; i < tot; ++i) {
            if (prime[i] > num) break;
            int j = i;
            int sum = 0;
            while (sum < num) {
                sum += prime[j++];
            }
            if (sum == num) count++;
        }
        return count;
    }

    void read() {
        sieve();
        while (true) {
            int num = ni();
            if (num == 0) break;
            out.println(solve(num));
        }
    }

    FastScanner in;
    PrintWriter out;

    void run() throws IOException {
        boolean oj;
        try {
            oj = ! System.getProperty("user.dir").equals("F:\\oxygen_workspace\\Algorithm");
        } catch (Exception e) {
            oj = System.getProperty("ONLINE_JUDGE") != null;
        }

        InputStream is = oj ? System.in : new FileInputStream(new File(INPUT));
        in = new FastScanner(is);
        out = new PrintWriter(System.out);
        long s = System.currentTimeMillis();
        read();
        out.flush();
        if (!oj){
            System.out.println("[" + (System.currentTimeMillis() - s) + "ms]");
        }
    }

    public boolean more(){
        return in.hasNext();
    }

    public int ni(){
        return in.nextInt();
    }

    public long nl(){
        return in.nextLong();
    }

    public double nd(){
        return in.nextDouble();
    }

    public String ns(){
        return in.nextString();
    }

    public char nc(){
        return in.nextChar();
    }

    class FastScanner {
        BufferedReader br;
        StringTokenizer st;
        boolean hasNext;

        public FastScanner(InputStream is) throws IOException {
            br = new BufferedReader(new InputStreamReader(is));
            hasNext = true;
        }

        public String nextToken() {
            while (st == null || !st.hasMoreTokens()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (Exception e) {
                    hasNext = false;
                    return "##";
                }
            }
            return st.nextToken();
        }

        String next = null;
        public boolean hasNext(){
            next = nextToken();
            return hasNext;
        }

        public int nextInt() {
            if (next == null){
                hasNext();
            }
            String more = next;
            next = null;
            return Integer.parseInt(more);
        }

        public long nextLong() {
            if (next == null){
                hasNext();
            }
            String more = next;
            next = null;
            return Long.parseLong(more);
        }

        public double nextDouble() {
            if (next == null){
                hasNext();
            }
            String more = next;
            next = null;
            return Double.parseDouble(more);
        }

        public String nextString(){
            if (next == null){
                hasNext();
            }
            String more = next;
            next = null;
            return more;
        }

        public char nextChar(){
            if (next == null){
                hasNext();
            }
            String more = next;
            next = null;
            return more.charAt(0);
        }
    }

    static class D{

        public static void pp(int[][] board, int row, int col) {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < row; ++i) {
                for (int j = 0; j < col; ++j) {
                    sb.append(board[i][j] + (j + 1 == col ? "\n" : " "));
                }
            }
            System.out.println(sb.toString());
        }

        public static void pp(char[][] board, int row, int col) {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < row; ++i) {
                for (int j = 0; j < col; ++j) {
                    sb.append(board[i][j] + (j + 1 == col ? "\n" : " "));
                }
            }
            System.out.println(sb.toString());
        }
    }

    static class ArrayUtils {

        public static void fill(int[][] f, int value) {
            for (int i = 0; i < f.length; ++i) {
                Arrays.fill(f[i], value);
            }
        }

        public static void fill(int[][][] f, int value) {
            for (int i = 0; i < f.length; ++i) {
                fill(f[i], value);
            }
        }

        public static void fill(int[][][][] f, int value) {
            for (int i = 0; i < f.length; ++i) {
                fill(f[i], value);
            }
        }
    }

    static class Num{
        public static <K> void inc(Map<K, Integer> mem, K k) {
            if (!mem.containsKey(k)) mem.put(k, 0);
            mem.put(k, mem.get(k) + 1);
        }
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017-12-07 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
POJ 刷题系列:3006. Dirichlet's Theorem on Arithmetic Progressions
用户1147447
2018/01/02
5370
POJ 刷题系列:3006. Dirichlet's Theorem on Arithmetic Progressions
POJ 刷题系列:1840. Eqs
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u014688145/article/details/79089056
用户1147447
2019/05/26
3390
POJ 刷题系列:3041. Asteroids
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u014688145/article/details/78985308
用户1147447
2019/05/25
3740
POJ 刷题系列:3295. Tautology
该文介绍了计算几何的一些基本概念和算法,包括线段、三角形、四面体、立方体、圆柱体和球体等。文章还介绍了图的遍历算法,包括深度优先搜索、广度优先搜索和迪杰斯特拉算法等。此外,文章还介绍了几个著名的计算机算法,如快速排序、归并排序和堆排序等。
用户1147447
2017/12/29
5300
POJ 刷题系列:3295. Tautology
POJ 刷题系列:1503 Integer Inquiry
用户1147447
2018/01/02
5270
POJ 刷题系列:1503 Integer Inquiry
POJ 刷题系列:2632. Crashing Robots
题意: 在一张地图(A x B)中给出N个robot的位置和初始移动方向,以及M条指令,任意两个robot不能同时移动,问是否存在非法的crash状态(撞墙,撞其他robot)。 思路:
用户1147447
2017/12/29
4900
POJ 刷题系列:2632. Crashing Robots
POJ 刷题系列:2993. Emag eht htiw Em Pleh
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u014688145/article/details/78866516
用户1147447
2019/05/26
2990
POJ 刷题系列:1068. Parencodings
用户1147447
2017/12/29
4800
POJ 刷题系列:1068. Parencodings
POJ 刷题系列:3299. Humidex
本文介绍了在 Java 并发编程中,通过使用 CountDownLatch、CyclicBarrier、Semaphore 三个类来实现线程同步等待和同步解除等待的机制。同时,还介绍了在 Spring Boot 中如何利用 AQS (AbstractQueuedSynchronizer) 实现线程安全的 WebClient 多任务并发执行。通过这些技术,可以有效地提高 Java 程序的性能和稳定性。
用户1147447
2018/01/02
5100
POJ 刷题系列:3299. Humidex
POJ 刷题系列:1573. Robot Motion
该文介绍了在Java中如何实现深度优先搜索算法。首先介绍了深度优先搜索算法的基本概念,然后详细讲解了如何在Java中实现该算法。最后,通过实例演示了如何使用深度优先搜索算法解决迷宫问题。
用户1147447
2017/12/29
4680
POJ 刷题系列:1573. Robot Motion
POJ 刷题系列:2965. The Pilots Brothers' refrigerator
根据给定的文章内容,撰写摘要总结。
用户1147447
2018/01/02
4970
POJ 刷题系列:2965. The Pilots Brothers' refrigerator
POJ 刷题系列:3080. Blue Jeans
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u014688145/article/details/79057958
用户1147447
2019/05/26
3880
POJ 刷题系列:1753. Flip Game
用户1147447
2018/01/02
7460
POJ 刷题系列:1753. Flip Game
POJ 刷题系列:2299. Ultra-QuickSort
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u014688145/article/details/79059221
用户1147447
2019/05/26
3130
挑战程序竞赛系列(79):4.3 2-SAT(3)
摘要总结:本文主要介绍了一种简化版的有限状态机实现,它使用Java语言实现,基于一个自定义的Util类进行状态转换,支持基于关键字驱动的状态机执行。该实现方式简化了有限状态机在实际问题中的应用,提高了开发效率和代码质量。
用户1147447
2018/01/02
5450
挑战程序竞赛系列(79):4.3  2-SAT(3)
POJ 刷题系列:2109. Power of Cryptography
本文通过分析题目2109,讨论了Power of Cryptography在解题中的应用,并提供了具体的代码实现。通过枚举题目中给出的矩阵,将整数数组转化为二叉树结构,利用二叉树的性质简化求解过程,并使用递归算法完成填充。最终通过实例演示了该方法的可行性。
用户1147447
2017/12/29
4080
POJ 刷题系列:2109. Power of Cryptography
2240. Arbitrage
思路: 在图模型中找负环即可。此处符合负环的特征如下:在负环上的顶点会不断更新最大值,所以在N轮没有停止更新就说明存在了负环。
用户1147447
2019/05/26
3610
POJ 刷题系列:2996. Help Me with the Game
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u014688145/article/details/78864960
用户1147447
2019/05/26
3590
POJ 刷题系列:2255. Tree Recovery
该文介绍了如何通过递归思想,使用一个递归函数构建一棵树,并利用一个数组来记录每个节点的值。在遍历树的过程中,每当遍历到一个节点时,都会将它的值写入数组中。最后,将数组转换为字符串,即可得到一棵完整的二叉树。
用户1147447
2018/01/02
6370
POJ 刷题系列:2255. Tree Recovery
3274. Gold Balanced Lineup
所以:求每一位置的累加和,可以快速求得任意区间内某个属性的个数,一个暴力的做法是遍历每个区间以及每个位置,判断每个属性个数是否相等。
用户1147447
2019/05/26
3880
相关推荐
POJ 刷题系列:3006. Dirichlet's Theorem on Arithmetic Progressions
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档