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: 欧拉计划是一系列挑战数学或者计算机编程问题,解决这些问题需要的不仅仅是数学功底. 启动这一项目的目的在于,为乐于探索的人提供一个钻研其他领域并且学习新知识的平台,将这一平 ...
随机推荐
- redis3.0.6安装配置
运行linux客户端 1 安装依赖 yum install gcc-c++ -y(安装gcc) 2 创建安装目录.编译.安装 mkdir -p /usr/local/redis(创建安 ...
- 我的Android进阶之旅------>Android字符串资源中的单引號问题error: Apostrophe not preceded by 的解决的方法
刚刚在string字符串资源文件里,写了一个单引號.报错了,错误代码例如以下 error: Apostrophe not preceded by \ (in OuyangPeng's blog ) 资 ...
- selenium获取html源代码
# 执行js得到整个HTML html = driver.execute_script("return document.documentElement.outerHTML") 获 ...
- [Exception Spring 1] - Attribute value must not be null
java.lang.IllegalArgumentException: Attribute value must not be null at org.springframework.util.Ass ...
- <译>Zookeeper官方文档
apache原文地址:http://zookeeper.apache.org/doc/trunk/zookeeperOver.html ZooKeeper ZooKeeper: A Distribut ...
- easyui datebox 扩展清空按钮
/** * 给时间框控件扩展一个清除的按钮 */ $.fn.datebox.defaults.cleanText = '清空'; (function ($) { var buttons = $.ext ...
- Tomcat版本历史
https://en.wikipedia.org/wiki/Apache_Tomcat Apache Tomcat versions Series Declared stable Descriptio ...
- java基础讲解03-----java的结构
前面我们说了java是面向对象的语言,java程序的基本组成单元是类,类中又属性,方法两个部分,每个应用程序都会有一个mian函数,含有main()方法的类,我们称为主类 package Test; ...
- win8安装Visual C++ 2015 build tools闪退解决办法
win8安装Visual C++ 2015 build tools闪退解决办法 安装Visual Studio 2015闪退问题也同样应用此解决办法. 1.控制面板——添加删除程序——启动关闭wind ...
- 计算机原理--cpu篇
简介 本文的目的是为了能够对特定的计算模型估算所需的CPU规格,个数. 这里主要介绍CPU的基本工作原理,指令集.(仅以X86体系结构的CPU为例 )