Light oj-1259 - Goldbach`s Conjecture
分拆素数和,本来很水的题,但硬生生跪了18+。这题卡内存实在太凶残了,MLT了10+,TLE了10+。
题意:在1e7内验证哥德巴赫猜想,求一个数拆分成两个不同的素数和有多少中方法。
思路:打表。就是打表。
但为什么会MLE呢,因为1e7的数组实在太大了,于是换了好几种写法优化,但又面临着TLE的囧境。在山穷水尽之际百度了一遍题解,坑爹竟然就是1e7的数组打表。但我为什么MLE了呢,于是找啊找,我用的int数组,初始化用的-1,但bool型的数组直接初始化为1。are you kidding me ?多次验证之后发现只有bool型的能过,其他的果断MLE。
那就是说:bool型的数组可以达到1e7,相比int型数组要更优一点。
const int N=1e7+10;
int len=0,b[N/10];//int 数组开这么大可能会被卡。
bool a[N];
void init()
{
memset(a,1,sizeof(a));
a[0]=a[1]=0;
for(int i=2; i<N; i++)
if(a[i])
{
b[len++]=i;
if(N/i<i) continue;
for(int j=i*i; j<N; j+=i) a[j]=0;
}
}
int main()
{
init();
int t,n,t1=1;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
int ans=0;
for(int i=0; i<len&&b[i]<=n/2; i++)
if(a[n-b[i]]) ans++;
printf("Case %d: %d\n",t1++,ans);
}
return 0;
}
哎,也算是学到了!!
Light oj-1259 - Goldbach`s Conjecture的更多相关文章
- LightOJ - 1259 - Goldbach`s Conjecture(整数分解定理)
链接: https://vjudge.net/problem/LightOJ-1259 题意: Goldbach's conjecture is one of the oldest unsolved ...
- LightOJ 1259 Goldbach`s Conjecture (哥德巴赫猜想 + 素数筛选法)
http://lightoj.com/volume_showproblem.php?problem=1259 题目大意:给你一个数n,这个数能分成两个素数a.b,n = a + b且a<=b,问 ...
- LightOJ 1259 Goldbach`s Conjecture 水题
不想说了 #include <cstdio> #include <iostream> #include <ctime> #include <vector> ...
- LightOJ 1259 Goldbach`s Conjecture 素数打表
题目大意:求讲一个整数n分解为两个素数的方案数. 题目思路:素数打表,后遍历 1-n/2,寻找方案数,需要注意的是:C/C++中 bool类型占用一个字节,int类型占用4个字节,在素数打表中采用bo ...
- LightOJ1259 Goldbach`s Conjecture —— 素数表
题目链接:https://vjudge.net/problem/LightOJ-1259 1259 - Goldbach`s Conjecture PDF (English) Statistic ...
- Goldbach`s Conjecture(LightOJ - 1259)【简单数论】【筛法】
Goldbach`s Conjecture(LightOJ - 1259)[简单数论][筛法] 标签: 入门讲座题解 数论 题目描述 Goldbach's conjecture is one of t ...
- Goldbach's Conjecture
Goldbach's Conjecture Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I ...
- Poj 2262 / OpenJudge 2262 Goldbach's Conjecture
1.Link: http://poj.org/problem?id=2262 http://bailian.openjudge.cn/practice/2262 2.Content: Goldbach ...
- poj 2262 Goldbach's Conjecture(素数筛选法)
http://poj.org/problem?id=2262 Goldbach's Conjecture Time Limit: 1000MS Memory Limit: 65536K Total ...
随机推荐
- 504 Base 7 七进制数
给定一个整数,将其转化为7进制,并以字符串形式输出.示例 1:输入: 100输出: "202" 示例 2:输入: -7输出: "-10"注意: 输入范围是 [- ...
- Centos 6.5安装MySQL-python
报错信息: Using cached MySQL-python-1.2.5.zip Complete output from command python setup.py egg_info: ...
- AJPFX关于一维数组的声明与初始化
一维数组:可以理解为一列多行.类型相同的数据,其中每个数据被称为数组元素:一维数组的声明方式: type varName[]; 或 type[] varName;(推荐) ...
- poj3436 Computer Factory
题意: 电脑公司生产电脑有N个机器,每个机器单位时间产量为Qi. 电脑由P个部件组成,每个机器工作时只能把有某些部件的半成品电脑(或什么都没有的空电脑)变成有另一些部件的半成品电脑或完整电脑(也可能移 ...
- 02html基础
02_html 1.几个标签 1.1 meta标签 meta标签的属性有name和http-equiv,其中name属性用于描述网页,对应于content(网页内容). <meta name=& ...
- 【HEVC简介】Inter Prediction Tools
参考文献:见<High Efficiency Video Coding (HEVC)>Inter-Picture Prediction in HEVC章节 <HEVC标准介绍.HEV ...
- qt QTableView/QTableWidget样式设置
转载请注明出处:http://www.cnblogs.com/dachen408/p/7591409.html 选中设置: QTableView::item:selected { background ...
- idea创建和部署tomcat项目
小编今天花费了一上午,参悟出了如何快速的在idea上面创建并部署一个属于自己的maven项目,很荣幸能将自己的开发经验推而广之,希望能够帮助到大家! 前言 小编参考博文: Intellij Idea ...
- Python基础3 函数 变量 递归 -DAY3
本节内容 1. 函数基本语法及特性 2. 参数与局部变量 3. 返回值 嵌套函数 4.递归 5.匿名函数 6.函数式编程介绍 7.高阶函数 8.内置函数 温故知新 1. 集合 主要作用: 去重 关系测 ...
- 转行做web前端,该如何进行短期快速自学,达到高新就业水平
就目前来说,毕业生如果想毕业就找到高薪的工作,互联网成为了第一个选择,在所有的职业中,不靠任何关系,全凭自己的能力就业,就是程序开发,而web前端开发是目最很热门的行业,在未来五年之内,web前端开发 ...