动态规划:求连续的元素的和(素数个)

//题意:

 Rhezo and Prime Problems
Attempted by:  3257
/
Accuracy:  77%
/
Maximum Score:  20
/
 
630 Votes
Tag(s):
 

Data Structures, Dynamic Programming, Easy, Number Theory

PROBLEM
EDITORIAL
MY SUBMISSIONS
ANALYTICS

Rhezo and his friend Vanya love problem solving. They have a problem set containing NN problems, with points assigned to each. Rhezo wants to solve problems in such a way that he gets the maximum number of points. Rhezo has a weird habit of solving only prime number of consecutive problems, that is, if he solves XX consecutive problems from the problem set, then XX should be prime. Vanya has already solved all problems from the problem set and he wonders how much maximum points Rhezo can get. Vanya can answer this, but can you?

Input:

First line contains a single integer NN, denoting the number of problems in the problem set. Next line contains NN space separated integers denoting the points assigned to the problems.

Output:

Print a single integer, the maximum points Rhezo can get.

Constraints:

1N50001≤N≤5000

11≤Points of Problems105≤105

SAMPLE INPUT
 
4
8 1 3 7
SAMPLE OUTPUT
 
12

//代码:

import java.util.*;
import java.io.*;
import java.math.*;


 class Main {
    public static void main(String args[]) {

        //File file= new File("COMPLETE_FILE_PATH");
        //InputStream is = new FileInputStream(file);
        //InputReader in = new InputReader(is);
        //InputReader in = new InputReader(System.in);
        //PrintWriter out = new PrintWriter(System.out);

        int t = 1;
        while (t-- > 0) {
            Task solver = new Task();
            solver.solve(t);
        }
    }
    static  class Task{
        long mod=1000000007;
        static boolean[]isPrime;
        static List<Integer>Prime;

    public static void solve(int testNumber)
    {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        long []arr=new long[n+1];
        long []dp=new long[n+1];
       for(int i=1;i<=n;i++)
       {
           arr[i]=arr[i-1]+sc.nextInt();
       }
       Prime=new ArrayList<>();
        judge(n);
       dp[0]=dp[1]=0;
      /*  for(int i=2;i<=n;i++)
            dp[i]=dp[i-1];*/
        for(int i=2;i<=n;i++)
        {
            dp[i]=dp[i-1];
            for(int j=0;j<Prime.size()&&Prime.get(j)<=i;j++){
                if(Prime.get(j)==i){
                    dp[i]=Math.max(dp[i],arr[i]);
                }
                else{
                    int p=i-Prime.get(j)-1;
                    dp[i]=Math.max(dp[i],dp[p]+arr[i]-arr[p+1]);
                }
            }
        }
        System.out.println(dp[n]);
    }
    public static void judge(int N)
    {
        isPrime=new boolean[N+1];
        for(int i=2;i<=N;i++){
            isPrime[i]=true;
        }
        for(int i=2;i<=N;i++){
            if(isPrime[i]){
                Prime.add(i);
            }
            for(int j=i*i;j<=N;j+=i){
                isPrime[j]=false;
            }
        }
    }
    }

}
https://doigetitnow.quora.com/Rhezo-and-Prime-problems

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值