Prime triplets (Project Euler 196)
hackerrank programming version
题目大意是定义了一个正整数的表,第一行是1,第二行是1,2,第三行1,2,3...定义prime triple是在表上八连通的三个质数。然后问某行有多少个质数至少在一个prime triple中。 行数 <= 1e7.
题解:
假设要求第n行的答案,只要把上2行和下2行的数都抠出来,然后判断一下就好了。问题转化为求[L,R]之间的质数,这里5行数,区间长度大概有5e7,所以要用modified 区间筛法。 参考 http://euler.stephan-brumme.com/196/ 。 没什么思维量,积累一个区间筛法模板。
#include <bits/stdc++.h>
using namespace std; typedef long long LL;
const int mod = 1e9 + ; char* isPrime;
LL bias; vector<LL> eratosthenesOddSingleBlock(LL from, LL to)
{
vector<LL> primes;
if (from > to || to <= ) return primes;
if (to == )
{
primes.push_back();
return primes;
}
if (!(from & )) ++from;
if (from > to) return primes; if (from <= ) primes.push_back(), from = ;
const int memorySize = (to - from) / + ;
char* isPrime = new char[memorySize]; for (int i = ; i < memorySize; i++)
isPrime[i] = ;
for (LL i = ; i*i <= to; i+=)
{
if (i >= * && i % == )
continue;
if (i >= * && i % == )
continue;
if (i >= * && i % == )
continue;
if (i >= * && i % == )
continue;
if (i >= * && i % == )
continue;
LL minJ = ((from+i-)/i)*i;
if (minJ < i*i)
minJ = i*i;
if ((minJ & ) == )
minJ += i;
for (LL j = minJ; j <= to; j += *i)
{
int index = j - from;
isPrime[index/] = ;
}
} for (int i = ; i < memorySize; i++)
if (isPrime[i]) primes.push_back(from + *i); delete[] isPrime;
return primes;
} bool checkPrime(LL val)
{
return isPrime[val - bias];
} inline LL getNumber(int r, int c)
{
return 1LL * (r - ) * r / + c;
} bool checkCentre(int r, int c)
{
if (c < || c > getNumber(r, c) || !checkPrime(getNumber(r, c))) return false;
int cnt = , x, y;
for (int dx = -; dx <= ; ++dx)
{
for (int dy = -; dy <= ; ++dy)
{
x = r + dx;
y = c + dy;
if (y < || y > x) continue;
cnt += checkPrime(getNumber(x, y));
if (cnt >= ) return true;
}
}
return false;
}
bool check(int r, int c)
{
for (int dx = -; dx <= ; ++dx)
for (int dy = -; dy <= ; ++dy)
if (checkCentre(r + dx, c + dy))
return true;
return false;
} LL solve(int row)
{
if (row == ) return ;
if (row <= ) return ; LL l = getNumber(row - , ), r = getNumber(row + , row + );
vector<LL> primes = eratosthenesOddSingleBlock(l, r);
isPrime = new char[r - l + ];
memset(isPrime, , r - l + );
bias = l;
for (auto p: primes) isPrime[p - bias] = ; pair<int, int> pos;
LL res = , val = getNumber(row, );
for (int col = ; col <= row; ++col)
{
if (checkPrime(val) && check(row, col))
res += val;
++val;
}
return res;
} int main()
{
//freopen("out.txt", "w" , stdout); int a, b;
cin >> a >> b;
cout << solve(a) + solve(b) << endl; return ;
}
Prime triplets (Project Euler 196)的更多相关文章
- Python练习题 035:Project Euler 007:第10001个素数
本题来自 Project Euler 第7题:https://projecteuler.net/problem=7 # Project Euler: Problem 7: 10001st prime ...
- Python练习题 031:Project Euler 003:最大质因数
本题来自 Project Euler 第3题:https://projecteuler.net/problem=3 # Project Euler: Problem 3: Largest prime ...
- [project euler] program 4
上一次接触 project euler 还是2011年的事情,做了前三道题,后来被第四题卡住了,前面几题的代码也没有保留下来. 今天试着暴力破解了一下,代码如下: (我大概是第 172,719 个解出 ...
- Python练习题 029:Project Euler 001:3和5的倍数
开始做 Project Euler 的练习题.网站上总共有565题,真是个大题库啊! # Project Euler, Problem 1: Multiples of 3 and 5 # If we ...
- Project Euler 9
题意:三个正整数a + b + c = 1000,a*a + b*b = c*c.求a*b*c. 解法:可以暴力枚举,但是也有数学方法. 首先,a,b,c中肯定有至少一个为偶数,否则和不可能为以上两个 ...
- Project Euler 44: Find the smallest pair of pentagonal numbers whose sum and difference is pentagonal.
In Problem 42 we dealt with triangular problems, in Problem 44 of Project Euler we deal with pentago ...
- project euler 169
project euler 169 题目链接:https://projecteuler.net/problem=169 参考题解:http://tieba.baidu.com/p/2738022069 ...
- 【Project Euler 8】Largest product in a series
题目要求是: The four adjacent digits in the 1000-digit number that have the greatest product are 9 × 9 × ...
- Project Euler 第一题效率分析
Project Euler: 欧拉计划是一系列挑战数学或者计算机编程问题,解决这些问题需要的不仅仅是数学功底. 启动这一项目的目的在于,为乐于探索的人提供一个钻研其他领域并且学习新知识的平台,将这一平 ...
随机推荐
- Android 微信支付,授权,分享回调区分记录
我们做项目中避免不了和微信打交道,其中最常用的也就是授权登录与分享和支付了. 本篇文章记录这三个功能同时使用的时候,回调怎么来区分.因为每个功能都有自己的回调状态.前期集成与发送,资料很多了就不在这里 ...
- leetcode第一刷_Combination Sum Combination Sum II
啊啊啊啊.好怀念这样的用递归保存路径然后打印出来的题目啊.好久没遇到了. 分了两种,一种是能够反复使用数组中数字的,一种是每一个数字仅仅能用一次的.事实上没有多大差别,第一种每次进入递归的时候都要从头 ...
- nginx做正向代理(Centos7,支持http和https)
默认的情况下,使用nginx做正向代理可以解析http请求, 对于诸如baidu.com这样的https请求,nginx默认并不支持,不过我们可以借助第三方模块来实现. 1.先说默认情况下的代理配置 ...
- mysql主从复制原理及实现
一.主从复制原理 利用MySQL提供的Replication,其实就是Slave从Master获取Binary log文件,然后再本地镜像的执行日志中记录的操作.由于主从复制的过程是异步的,因此Sla ...
- swift2.0中文版教程
有些同学问我要swift的中文版教程,为了节省大家的找资料的时间,我就把我网上下载的PDF放到这里共享好了. 点击链接或者右击选择下载文件进行下载:swift2.0中文版教程 在此也感谢翻译者们的贡献 ...
- 使用swagger实现在线api文档自动生成 在线测试api接口
使用vs nuget包管理工具搜索Swashbuckle 然后安装便可 注释依赖于vs生成的xml注释文件
- UIPopoverController具体解释
今天一位童鞋问我个问题.大意是popoverController不会显示.经过我寻找问题发现以下这种方法不好掌控. 为什么说他不好掌控那.我这个给大家带来一个列子.通过这个列子来介绍PopoverCo ...
- lnmp1.4环境FTP服务器的安装和使用
首先还是用Xshell连接到VPS界面,进入lnmp解压后的目录,命令如下: cd lnmp1.4 然后安装FTP服务器,命令如下: ./pureftpd.sh 看到提示Press any ...
- python-循环while
while 只要…条件成立,就一直做…. for 循环会在可迭代的序列被穷尽的时候停止,while 则是在条件不成立的时候停止,因此 while 的作用概括成一句话就是:只要…条件成立,就一直做…. ...
- win10 清理winsxs文件夹
dism /online /cleanup-image /startcomponentcleanup /resetbase