题目大意:

对于连续的质数$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. sersync部署

    rsync :  wget  http://rsync.samba.org/ftp/rsync/src/rsync-3.1.1.tar.gz Sersync: wget https://raw.git ...

  2. windows 7系统搭建PHP网站环境

    2.新建数据库打开浏览器,输入http://localhost:9999或者http://127.0.0.1:9999回车填写用户名root和密码回车登录点击权限-添加新用户填写用户名,主机选择本地, ...

  3. jsp标签、 项目全路径引用${ctx}

    请根据自己的需要选择以下标签. <%@ taglib uri="/struts-tags" prefix="s"%> <%@ taglib u ...

  4. idea Tomcat 部署 war和war exploded的区别

    idea Tomcat 部署 war和war exploded的区别 学习了:https://blog.csdn.net/linjpg/article/details/73322881 explode ...

  5. MS project 使用小技巧收集

    如何使用的url: http://www.cnblogs.com/wangfupeng1988/p/3648994.html  (好文,易上手) 一.  如何设置周末为工作日. 1. 在 “工具”-& ...

  6. Block系列2:Block内存管理

    ViewController.h #import <UIKit/UIKit.h> @interface ViewController : UIViewController { UIImag ...

  7. mybatis 自动更新表结构 ,兼容通用tkmapper

    1.maven引入jar <dependency> <groupId>com.github.gonglb.tools</groupId> <artifactI ...

  8. Mac OS X中配置Apache后提示You don't have permission to access / on this server

    根据这篇博客http://www.cnblogs.com/snandy/archive/2012/11/13/2765381.html,在mac系统中,配置的apache,配置完成后,提示 You d ...

  9. ios 自动布局水平跟垂直居中

    [view addConstraint:[NSLayoutConstraint constraintWithItem:segment attribute:NSLayoutAttributeCenter ...

  10. 【转载】HTTP和SOAP完全就是两个不同的协议

    http:是一个客户端和服务器端请求和应答的标准(TCP). http协议其目的是为了提供一种发布和接收htttp页面的方法 http协议的客户端与服务器的交互:由HTTP客户端发起一个请求,建立一个 ...