UVa 10742 - The New Rule in Euphomia
来源:互联网 发布:淘宝需要注册公司吗 编辑:程序博客网 时间:2024/05/18 22:47
题目:给你一个数,问有多少对素数的和小于这个数。
分析:数论。利用素数表枚举求解。
1.打表计算1000000内的素数(筛法);
2.对于每个输入的数字 v,枚举他的所有拆分情况;
设F(i)是不超过数字 i 的素数的个数,S(v)为 v 的拆分种数,则:
S(v)= sum(min(F(i),v - F(i)+ 1 )) { 1 < i < v }
说明:算了一下最坏情况会超时(600组都是1000000),结果跑了198ms (⊙_⊙)
#include <iostream>#include <cstdlib>#include <cstring>#include <cstdio>#define min(a,b) (a)<(b)?(a):(b)using namespace std;int visit[1000001];int prime[1000001];int F[1000001];int bs( int r, int key ){int mid,l = 0;while ( l < r ) {mid = l+(r-l+1)/2;if ( prime[mid] >= key )r = mid-1;else l = mid;}return l;}int main(){int count = 1;for ( int i = 0 ; i < 1000001 ; ++ i )visit[i] = F[i] = 0;prime[0] = 0;for ( int i = 2 ; i < 1000001 ; ++ i )if ( !visit[i] ) {prime[count ++] = i;for ( int j = i*2 ; j < 1000001 ; j += i )visit[j] = 1;}int value,sum,T = 1;while ( scanf("%d",&value) && value ) {sum = 0;for ( int i = 2 ; prime[i] < value ; ++ i )sum += bs( count-1, min( prime[i], value-prime[i]+1 ) );printf("Case %d: %d\n",T ++,sum);}return 0;}
0 0
- UVa 10742 - The New Rule in Euphomia
- NEW RULE FOR THE NEW ECONOMY
- uva 321 - The New Villa
- UVa 321 - The New Villa
- UVA 321 - The New Villa
- uva 321 - The New Villa
- uva 321 The New Villa
- UVa 321 - The New Villa
- UVA - 321 The New Villa
- the new thing in webkit3
- uva 321 - The New Villa bfs+递归
- uva:321 - The New Villa(bfs + 哈希判重)
- The New Villa -uva 隐式图搜索
- UVa 321 & ZOJ 1301 - The New Villa
- UVA - 321 The New Villa 隐式图搜索
- The New Villa (Uva 321 bfs)
- UVa 11968 - In The Airport
- New residence rule gives hope
- 一点php的知识
- 快速开发11之多次点击事件实现功能的万能代码
- 检测一个二叉树是否是另一个二叉树的子树
- StringBuilder
- 基本数据类型对象包装类
- UVa 10742 - The New Rule in Euphomia
- 关于Android屏幕横竖之间的切换,研究心得。
- 基本数据类型对象包装类新特性
- 集合框架(体系概述)-共性方法-迭代器
- javaScript中闭包函数与this对象的应用
- openstack root-wrap
- MiniGUI实践之PhotoView
- 采用pull解析xml文件
- hdu3072 强联通水题