Prime pair connection (Project Euler 134)
题目大意:
对于连续的质数$p1$, $p2$, 满足$5 <= p1 <= 1000000$ 求出最小的整数$S$, 它以 $p1$结尾并且能够被$p2$整除。 求$S$的和。
思路:
只需要知道对于一对$p1$, $p2$怎么求对应的$S$. 把$S$表示成$x*10^k+p1$ 其中$k$是$p1$的长度。
然后就转化为求同余方程 $x*10^k+p1\equiv 0\ (mod\ p2)$
代码:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <set>
#include <cstring>
#include <map>
#include <queue>
using namespace std; typedef long long ll;
#define N 10000000
#define M 1100
typedef pair<int,int> pii; bool flag[N];
int p[N],phi[N]; void Get_Primes(int lim)
{
phi[]=;
for (int i=;i<=lim;i++)
{
if (!flag[i]) p[++p[]]=i,phi[i]=i-;
for (int j=;j<=p[] && i*p[j]<=lim;j++)
{
flag[i*p[j]]=true;
if (i%p[j]==)
{
phi[i*p[j]]=phi[i]*p[j];
break;
}
else phi[i*p[j]]=phi[i]*(p[j]-);
}
}
} ll Power(ll x, ll P, ll mod)
{
ll res = ;
for (; P ; P >>= )
{
if (P & ) res = res * x % mod;
x = x * x % mod;
}
return res;
} ll Solve(ll p1, ll p2)
{
ll tmp = p1, t = ;
while (tmp) tmp /= , t *= ;
ll x = (p2 - p1) * Power(t, p2 - , p2) % p2;
return x * t + p1;
} int main()
{
freopen("in.in","r",stdin);
freopen("out.out","w",stdout); ll res = ;
Get_Primes();
for (int i = ; p[i] <= ; ++i) res += Solve(p[i], p[i + ]);
cout << res << endl;
return ;
}
答案:18613426663617118
Prime pair connection (Project Euler 134)的更多相关文章
- 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 ...
- Python练习题 048:Project Euler 021:10000以内所有亲和数之和
本题来自 Project Euler 第21题:https://projecteuler.net/problem=21 ''' Project Euler: Problem 21: Amicable ...
- 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 8】Largest product in a series
题目要求是: The four adjacent digits in the 1000-digit number that have the greatest product are 9 × 9 × ...
随机推荐
- python函数getopt用法
python内建模块,用来处理命令行参数 格式:getopt(args, shortopts, longopts = []) 参数args一般是sys.argv[1:]sys.argv[0]表示执行文 ...
- DWZ(一):框架初了解
DWZ富client框架(jQuery RIAframework),是中国人自己开发的基于jQuery实现的Ajax RIA开源框架. DWZ富client框架设计目标是简单有用.扩展方便.高速开发. ...
- java获取src下包的文件的路径
String params = getClass().getClassLoader().getResource("system-config.properties").getPat ...
- solr6.6 solrJ索引富文本(word/pdf)文件
1.文件配置 在core下面新建lib文件夹,存放相关的jar包,如图所示: 修改solrconfig.xml <lib dir="${solr.install.dir:../../. ...
- 【千纸诗书】—— PHP/MySQL二手书网站后台开发之功能实现
前言:前一篇温习了网站开发需要掌握的基础知识,这一篇重点梳理一下各个功能模块的[详细设计与实现].项目github地址:https://github.com/66Web/php_book_store, ...
- web开发者的博客
一. Jerry Qu https://imququ.com/post/sth-about-switch-to-https-2.html 点评:博客做的比较好,样式不错. http知识比较深.
- REOBJECT structure
REOBJECT structure 包含丰富编辑控件中的OLE或图像对象的信息. Syntax 语法 typedef struct _reobject { DWORD cbStruct; LON ...
- Python——标准库 Sys模块
---------------------------------------------------------------------------------------------------- ...
- Win10 系统变成黑白屏幕了怎么办?
快捷键"windows徽标键+Ctrl+C"可以切换屏幕黑白了 这是win自带的一个"应用颜色筛选器", 桌面右键,选择个性化 点到背景页面,下拉,点击&q ...
- 【高德地图API】从零開始学高德JS API(二)地图控件与插件——測距、圆形编辑器、鼠标工具、地图类型切换、鹰眼鱼骨
不管是控件还是插件,都是在一级API接口的基础上,进行二次开发,封装的一系列更加便于开发人员使用.降低开发人员工作量的二级API接口.除了官方通用的鱼骨.鹰眼控件,还有大量官方开发的地图插件,相似谷歌 ...