Sum of Consecutive Prime Numbers UVA
来源:互联网 发布:djvu mac 打开 编辑:程序博客网 时间:2024/06/05 09:45
Think:
1题意:输入正整数n(2 <= n <= 10000),求连续素数之和等于n的方案数
2思路:
1>筛法建立素数表+初级dp思想(从后往前规划得到当前素数可连续组合得到的数值)
2>参考博客:“素数表+区间滑动”,“素数表+区间枚举”
vjudge题目链接
以下为Accepted代码
#include <bits/stdc++.h>using namespace std;int tp, link[10400], dp[10400];void Init_prime();void Init_dp();int main(){ Init_prime(); Init_dp(); int n; while(scanf("%d", &n) && n){ printf("%d\n", dp[n]); } return 0;}void Init_prime(){ int v[10400]; tp = 0; memset(v, 0, sizeof(v)); v[1] = 1, v[2] = 0; for(int i = 2; i <= 10000; i++){ if(!v[i]){ link[tp++] = i; for(int j = i*2; j <= 10000; j += i){ v[j] = 1; } } }}void Init_dp(){ memset(dp, 0, sizeof(dp)); for(int i = tp-1; i >= 0; i--){ int t = 0; for(int j = i; j >= 0; j--){ if(t + link[j] <= 10000){ t += link[j]; dp[t] += 1; } else break; } }}
阅读全文
1 0
- 【UVA】1210 - Sum of Consecutive Prime Numbers
- UVa 1210 - Sum of Consecutive Prime Numbers
- UVa 1210 - Sum of Consecutive Prime Numbers
- Uva-1210 Sum of Consecutive Prime Numbers
- UVa 1210 - Sum of Consecutive Prime Numbers
- UVA - 1210 - Sum of Consecutive Prime Numbers
- Sum of Consecutive Prime Numbers UVA
- Sum of Consecutive Prime Numbers UVA
- Sum of Consecutive Prime Numbers UVA
- Sum of Consecutive Prime Numbers
- Sum of Consecutive Prime Numbers
- Sum of Consecutive Prime Numbers
- Sum of Consecutive Prime Numbers
- Sum of Consecutive Prime Numbers
- Sum of Consecutive Prime Numbers
- Sum of Consecutive Prime Numbers
- Sum of Consecutive Prime Numbers - UVa 1210 水题
- UVa 1210 - Sum of Consecutive Prime Numbers(滑动窗口)
- 65、66、67
- fastjson 解析json
- 文章标题JavaWeb: 报错信息The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Pat
- codeforces——797B——Odd sum
- 调用类自身常量
- Sum of Consecutive Prime Numbers UVA
- 高斯混合模型(GMM)
- C++ Primer Plus, Chapter 14, excercise
- 决策树学习笔记
- WM9876声卡驱动框架
- HDU5093 Battle ships (BZOJ4554)
- UVA 567—Risk
- 杂粮和面包屑
- 电脑不能上网