题目大意:

对于连续的质数$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)的更多相关文章

  1. 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 ...

  2. project euler 169

    project euler 169 题目链接:https://projecteuler.net/problem=169 参考题解:http://tieba.baidu.com/p/2738022069 ...

  3. Python练习题 048:Project Euler 021:10000以内所有亲和数之和

    本题来自 Project Euler 第21题:https://projecteuler.net/problem=21 ''' Project Euler: Problem 21: Amicable ...

  4. Python练习题 035:Project Euler 007:第10001个素数

    本题来自 Project Euler 第7题:https://projecteuler.net/problem=7 # Project Euler: Problem 7: 10001st prime ...

  5. Python练习题 031:Project Euler 003:最大质因数

    本题来自 Project Euler 第3题:https://projecteuler.net/problem=3 # Project Euler: Problem 3: Largest prime ...

  6. [project euler] program 4

    上一次接触 project euler 还是2011年的事情,做了前三道题,后来被第四题卡住了,前面几题的代码也没有保留下来. 今天试着暴力破解了一下,代码如下: (我大概是第 172,719 个解出 ...

  7. Python练习题 029:Project Euler 001:3和5的倍数

    开始做 Project Euler 的练习题.网站上总共有565题,真是个大题库啊! # Project Euler, Problem 1: Multiples of 3 and 5 # If we ...

  8. Project Euler 9

    题意:三个正整数a + b + c = 1000,a*a + b*b = c*c.求a*b*c. 解法:可以暴力枚举,但是也有数学方法. 首先,a,b,c中肯定有至少一个为偶数,否则和不可能为以上两个 ...

  9. 【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 × ...

随机推荐

  1. jquery.mobile 中 collapsible-set collapsible listview 共同布局问题

    最近项目用上了jquery.mobile这货,在手机上做点简单的显示.之前只知道有这个框架,没把玩过. 特别是事件绑定方面,相比桌面系统下浏览器用着各种不爽,不得要领. 如下图,在做后台系统时,一般左 ...

  2. 一个 关于 case when的SQL

    [例]从stud_grade表中查询所有同学考试成绩情况,凡成绩为空者输出“未考”.小于60分输出“不及格”.60分至70分输出“及格”.70分至90分输出“良好”.大于或等于90分时输出“优秀”. ...

  3. Java平时需要注意的事项

    1.String 相等 稍微有点经验的程序员都会用equals比较而不是用 ==,但用equals就真的安全了吗,看下面的代码 user.getName().equals("xiaoming ...

  4. 实现Xshell断开连接情况下Linux命令继续执行

    1.将原命令语句改为:nohup 命令语句 & 2.回车执行,再回车,窗口中会显示一个进程号 3.如果中途想关闭,可执行:kill -9 进程号.如果想查看命令执行情况,可执行:cat noh ...

  5. (转)Netty : writeAndFlush的线程安全及并发问题

    rocketmq用netty实现的网络连接,发现它多个线程掉用一个channel连接,所以这个是线程安全的? 使用Netty编程时,我们经常会从用户线程,而不是Netty线程池发起write操作,因为 ...

  6. python之MySQL学习——防止SQL注入

    python之MySQL学习——防止SQL注入 学习了:https://www.cnblogs.com/xiaomingzaixian/p/7126840.html https://www.cnblo ...

  7. &lt;LeetCode OJ&gt; 101. Symmetric Tree

    101. Symmetric Tree My Submissions Question Total Accepted: 90196 Total Submissions: 273390 Difficul ...

  8. Foundation框架 - NSNumber类

    NSNumber类 NSFormatter #import <Foundation/Foundation.h> int main(int argc, const char * argv[] ...

  9. c# word 操作

    public class WordOperate { #region 新建Word文档 /// <summary> /// 动态生成Word文档并填充内容 /// </summary ...

  10. iOS学习笔记之蓝牙(有关蓝牙设备mac地址处理) 2

    1.创建中心设备,并设置代理 一般情况下,手机是中心设备,蓝牙设备是外围设备. self.centralManager = [[CBCentralManager alloc] initWithDele ...