习题10-6 UVA - 1210 Sum of Consecutive Prime Numbers 连续素数之和(滑动窗口)
来源:互联网 发布:yunos自动删除软件 编辑:程序博客网 时间:2024/05/16 08:25
大体题意:
给你一个整数n(n <= 10000),有多少种方案可以把n 写成若干个连续素数之和?
思路:
直接素数打表,然后滑动窗口即可!
当sum 小于目标时,移动右边r。
发现达到目标,直接记录答案。
当sum 大于目标时,直接减左边的l
最后输出ans 即可!
#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>using namespace std;const int maxn = 100000 + 10;int vis[maxn];int prime[maxn],n,cnt;void init(){ int m = sqrt(maxn + 0.5); for (int i = 2; i <= m; ++i) if (!vis[i]) for (int j = i * i; j <= maxn; j += i)vis[j] = 1; for (int i = 2; i < maxn; ++i)if (!vis[i])prime[cnt++] = i;// for (int i = 0; i < 100; ++i)printf("%d\n",prime[i]);}int solve(){ int l =0,r = 0; int sum = 0; int ans = 0; while(1){ while(sum < n && prime[r] <=n){ sum += prime[r++]; } if (l > r)return ans; if (sum < n)return ans; if (sum == n){ ++ans;// printf("%d %d\n",l,r); sum -= prime[l++]; } while(sum > n){ sum -= prime[l++]; } if (sum > n)return ans; }}int main(){ init(); while(scanf("%d",&n) == 1 && n) printf("%d\n",solve()); return 0;}
0 0
- 习题10-6 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
- 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(数论)
- poj 2739 Sum of Consecutive Prime Numbers (最大连续素数和)
- POJ 2739 Sum of Consecutive Prime Numbers-数论-(连续素数和)
- POJ 2739 Sum of Consecutive Prime Numbers(连续素数和)
- POJ 2739 Sum of Consecutive Prime Numbers(素数打表水题)
- poj2739 Sum of Consecutive Prime Numbers (素数打表)
- Sum of Consecutive Prime Numbers UVA
- Sum of Consecutive Prime Numbers UVA
- java源码分析(4)-AbstractStringBuilder
- iOS的事件传递响应链
- linked-list-cycle(LeetCode)
- 关于AVCaptureDevice
- Android发送邮件到指定邮箱(可带附件)
- 习题10-6 UVA - 1210 Sum of Consecutive Prime Numbers 连续素数之和(滑动窗口)
- Linux Char-Driver (字符驱动 摘要)(一)
- 数据挖掘方法比较
- Java之——使用JMX监控Tomcat
- 一个不怕机器识别的图形验证码方案
- Android自动化测试之Robotium学习(二)
- Logistic回归总结
- Middle-题目84:82. Remove Duplicates from Sorted List II
- 4.自定义View