标签:ber pst imp compose 尺取法 cat r++ epo 子序列
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 23931 | Accepted: 13044 |
Description
Input
Output
Sample Input
2 3 17 41 20 666 12 53 0
Sample Output
1 1 2 3 0 0 1 2
思路:尺取法操作连续子序列。
import java.util.Arrays; import java.util.Scanner; public class Main { Scanner in = new Scanner(System.in); final int MAXN = 10005; int[] prime = new int[MAXN]; boolean[] isPrime = new boolean[MAXN]; int[] sum = new int[MAXN]; int total; void table() { Arrays.fill(isPrime, true); isPrime[0] = false; isPrime[1] = false; for(int i = 2; i < MAXN; i++) { if(isPrime[i]) { prime[total++] = i; for(int j = i + i; j < MAXN; j += i) { isPrime[j] = false; } } } sum[0] = 0; for(int i = 1; i < total; i++) { sum[i] = sum[i-1] + prime[i-1]; } } Main() { int n; table(); while((n = in.nextInt()) != 0) { int res = 0, sum = 0; int front = 0, rear = 0; while(true) { while(rear < total && prime[rear] <= n && sum < n) { sum += prime[rear++]; } if(sum == n) { res++; } sum -= prime[front++]; if(front >= total || front > rear) { break; } } System.out.println(res); } } public static void main(String[] args) { new Main(); } }
标签:ber pst imp compose 尺取法 cat r++ epo 子序列
原文地址:http://www.cnblogs.com/program-ccc/p/6111923.html