B: Break Prime】的更多相关文章

题目描述 给定一个素数,试判断能否将该素数写为b3−a3 的形式,a,b皆为非负整数. 输入 多组输入 每行一个素数P (2≤P≤1015)   输出 若可以分解输出a,b(a<b) ,不能输出-1 -1: 样例输入 19 样例输出 2 3 题解:用公式降幂:a3-b3=(a-b)(a2+b2+ab);因为a3-b3=素数,所以a-b=1;所以解a2+b2+ab=p就行注意:用cin,cout会超时 #include <iostream> #include<stdio.h>…
It is possible to write ten as the sum of primes in exactly five different ways: 7 + 3 5 + 5 5 + 3 + 2 3 + 3 + 2 + 2 2 + 2 + 2 + 2 + 2 What is the first value which can be written as the sum of primes in over five thousand different ways? #include <i…
题意: 素数41可以写成六个连续素数的和: 41 = 2 + 3 + 5 + 7 + 11 + 13 在小于一百的素数中,41能够被写成最多的连续素数的和. 在小于一千的素数中,953能够被写成最多的连续素数的和,共包含连续21个素数. 在小于一百万的素数中,哪个素数能够被写成最多的连续素数的和? 思路:首先打出100000以内的素数表,然后计算出所有从第一个素数到 j 的和,然后枚举两个端点判断是否符合要求即可 /****************************************…
O(n)线性筛选n以内的素数 (1)对于任何一个素数p,都不可能表示为两个数的乘积 (2)对于任何一个合数m = p1a1p2a2…pmam,这里p1< p2 < … <pm,都能使用p1a1-1p2a2…pmam* p1进行筛选 fillchar(prime,sizeof(prime),); prime[]:=false; fillchar(p,sizeof(p),); total:=; to n do begin if prime[i] then begin inc(total);p…
var tot,i,j,k,m,n:longint; prime:array[0..100000] of boolean; p:array[0..100000] of longint;begin read(n); fillchar(prime,sizeof(prime),true); prime[1]:=false; tot:=0; fillchar(p,sizeof(p),0); for i:=2 to n do begin if prime[i] then begin inc(tot); p…
题意: 给你n个数,让你从中选一个子集要求子集中的任何两个数相加都是质数. 思路: 一开始把自己坑了,各种想,后来发现一个简单的性质,那就是两个数相加的必要条件是这两个数之中必定一个奇数一个偶数,(除了含有1 集合以外,1+1等于2也是质数). 考虑两种情况,有1存在和1不存在这两种. 很显然1存在的情况下,所有的1都可以同时在集合中出现,要想集合最大不能加奇数,只能加偶数,那么我们看原始集合中是否有偶数加一是素数. 不考虑1的情况下,这样的子集最大是2,只有存在一个奇数一个偶数相加是质数的情况…
前记: TM终于决定以后干啥了.这几天睡的有点多.困饿交加之间喝了好多水.可能是灌脑了. 切记两件事: 1.安心当单身狗 2.顺心码代码 题意: 给你N种颜色的珠子,串一串长度问N的项链,要求旋转之后重合的算是同一种项链.问一共有多少中可能.结果模p. 1 <= N <= 1000000000, 1 <= P <= 30000 思路: 首先是我们的POLYA定理,给定的公式是这样的sigma(N^gcd(N,i))/N   i从0到N-1. 然后是优化的问题.因为如果我们枚举i累加…
Description 有一张N×m的数表,其第i行第j列(1 < =i < =礼,1 < =j < =m)的数值为 能同时整除i和j的所有自然数之和.给定a,计算数表中不大于a的数之和. Input 输入包含多组数据.     输入的第一行一个整数Q表示测试点内的数据组数,接下来Q行,每行三个整数n,m,a(|a| < =10^9)描述一组数据. Output 对每组数据,输出一行一个整数,表示答案模2^31的值. Sample Input 2 4 4 3 10 10 5…
Description 根据一些书上的记载,上帝的一次失败的创世经历是这样的: 第一天, 上帝创造了一个世界的基本元素,称做“元”. 第二天, 上帝创造了一个新的元素,称作“α”.“α”被定义为“元”构成的集合.容易发现,一共有两种不同的“α”. 第三天, 上帝又创造了一个新的元素,称作“β”.“β”被定义为“α”构成的集合.容易发现,一共有四种不同的“β”. 第四天, 上帝创造了新的元素“γ”,“γ”被定义为“β”的集合.显然,一共会有16种不同的“γ”. 如果按照这样下去,上帝创造的第四种元…
Description N children are sitting in a circle to play a game. The children are numbered from 1 to N in clockwise order. Each of them has a card with a non-zero integer on it in his/her hand. The game starts from the K-th child, who tells all the oth…
/*** 大意:计算gcd(x,y,z) =1 0<= x, y , z <= n 问有多少个这样的对 莫比乌斯反演:(反演: 用结果推原因) 函数m(m)的定义如下: 莫比乌斯反演: * f(x) = sigma{g(d)}其中x % d == 0,则g(x) = sigma{miu(d) * f(x/d)} * f(x) = sigma{g(d)}其中d % x == 0,则g(x) = sigma{miu(d/x) * f(d)} 莫比乌斯反演中miu(x) = * 1 {x中含有偶数个…
1. Two Sum Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input would have exactly one solution. Example: Given nums = [2, 7, 11, 15], target = 9, Because nums[0] + n…
#include <iostream> #include <cstdio> #include <queue> #include <cstring> #include <algorithm> #include <cmath> using namespace std; #define N 1100 #define INF 0x7fffffff bool prime[N]; void init() { memset(prime, true,…
因为今天省选组也做a组,以为今天a组会很难,就做了做b组.t1和t3强行暴力,好在有t2保底.t1和正解就差一点,然而考试时死活想不起来...... 今天改题可以少改一道了!ovo 救救孩子吧!t1T到上天......然后打完后电脑蓝屏了.不想动弹 好吧,重打了一遍,终于调对了. t1: Description 有n个正整数a[i],设它们乘积为p,你可以给p乘上一个正整数q,使p*q刚好为正整数m的阶乘,求m的最小值.   Input 共两行.第一行一个正整数n.第二行n个正整数a[i]. O…
欧拉函数 ψ(x)=x*(1-1/pi)  pi为x的质数因子 特殊性质(图片内容就是图片后面的文字) 欧拉函数是积性函数——若m,n互质, ψ(m*n)=ψ(m)*ψ(n): 当n为奇数时,   ψ(2*n)=ψ(n); 若n为质数则                                                                 ψ(n)=n-1; 代码实现求欧拉函数 复杂度O(n*n) (有优化会补上) #include<bits/stdc++.h> #de…
Deciphering Password Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2357    Accepted Submission(s): 670 Problem Description Xiaoming has just come up with a new way for encryption, by calculati…
传送门 代码中体现在那个 $ break $ $ prime $ 数组 中的素数是递增的,当 $ i $ 能整除 $ prime[j ] $ ,那么 $ iprime[j+1] $ 这个合数肯定被 $ prime[j] $ 乘以某个数筛掉. 因为i中含有 $ prime[j] $ , $ prime[j] $ 比 $ prime[j+1] $ 小.接下去的素数同理.所以不用筛下去了. 在满足 $ i%prme[j]==0 $ 这个条件之前以及第一次满足改条件时, $ prime[j] $ 必定是…
#include<stdio.h> #include<iostream> #include<queue> #include<memory.h> #include <math.h> #include<time.h> #include <stdlib.h> using namespace std; * ; * ; /** * 欧拉素数筛选法 */ int prime[MAX]; ; void primes() { memset…
早上起床太晚,最后没时间了.. 不是ac代码,下次题目在oj上线的时候再去做一下.. #include<iostream> #include<cstdio> #include<cstring> #include<vector> #include<algorithm> #include<cmath> #include<set> using namespace std; ]; int getHigh(int a) { ) {…
题目链接 洛谷 狗粮版 前置技能 初中基础的因式分解 线性筛 \(O(nlog)\)的分解质因数 唯一分解定理 题解 首先来分解一下式子 \[\frac{1}{x}+\frac{1}{y}=\frac{1}{n!}\] 通分可化为: \[\frac{x+y}{xy}=\frac{1}{n!}\] 两边同时乘\(xy*(n!)\) \[(x+y)n!=xy\] 移项得: \[xy-(x+y)n!=0\] 两边同时加上\((n!)^2\) \[xy-(x+y)n!+(n!)^2=(n!)^2\] 通…
题意 给一个\(n\),计算 \[\sum_{i=1}^{n}\sum_{j=1}^{i-1}[gcd(i + j, i - j) = 1]\] 题解 令\(a = i - j\) 要求 \[\sum_{i=1}^{n}\sum_{j=1}^{i-1}[gcd(i + j, i - j) = 1]\] 即求 \[\sum_{i=1}^{n}\sum_{a=1}^{i-1}[gcd(2*i - a, a) = 1]\] 根据\(gcd\)的性质,即 \[\sum_{i=1}^{n}\sum_{a=…
题意: 欧拉发现了这个著名的二次多项式: f(n) = n2 + n + 41 对于连续的整数n从0到39,这个二次多项式生成了40个素数.然而,当n = 40时402 + 40 + 41 = 40(40 + 1) + 41能够被41整除,同时显然当n = 41时,412 + 41 + 41也能被41整除. 随后,另一个神奇的多项式n2 − 79n + 1601被发现了,对于连续的整数n从0到79,它生成了80个素数.这个多项式的系数-79和1601的乘积为-126479. 考虑以下形式的二次多…
题意: 首次出现连续两个数均有两个不同的质因数是在: 14 = 2 × 715 = 3 × 5 首次出现连续三个数均有三个不同的质因数是在: 644 = 22 × 7 × 23645 = 3 × 5 × 43646 = 2 × 17 × 19 首次出现连续四个数均有四个不同的质因数时,其中的第一个数是多少? 方法一:普通筛法 /************************************************************************* > File Name…
一个坑: 有组数据如下: 1 1 坑很深-- //By SiriusRen #include <cstdio> #define N 200000 using namespace std; int n,mindiv[200050],prime[100000],top=0,xx,rec; int main(){ for(int i=2;i<=N;i++) { if(!mindiv[i])prime[++top]=mindiv[i]=i; for(int j=1;j<=top&&…
很高兴给大家出题,本次难度低于上一场,新生的六个题都可以直接裸递归式或者裸递推式解决,对于老生的汉诺塔3,需要找出一般式,后两题分别为裸ST算法(或线段树)/线性DP. 正确的难度顺序为 种花 角谷定律 猴子和椰子 汉诺塔1 汉诺塔2 整数划分 跳台阶 汉诺塔3 夏目友人帐(一) 夏目友人帐(二) 一.种花 本题很容易能推出递推式或者一般式,对于第一快地,有3种种植方法,对于后面的每一快地有不同于前一块地的两种种植方法. a1 = 3: an = 2*(an-1): 代码: #include <…
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2161 题意:判断n是不是素数,输入到0停止.题目规定1 2 都不是素数. 题解:筛素数.老题目.不过这次是普通筛23333..之前做的题了. #include<iostream> #include<cmath> #include<cstdio> using namespace std; #define N 16001 bool prime[N]; void init(){…
Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20050   Accepted: 10989 Description Some positive integers can be represented by a sum of one or more consecutive prime numbers. How many such representatio…
hdu5901题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5901 code vs 3223题目链接:http://codevs.cn/problem/3223/ 思路:主要是用了一个Meisell-Lehmer算法模板,复杂度O(n^(2/3)).讲道理,我不是很懂(瞎说什么大实话....),下面输出请自己改 #include<bits/stdc++.h> using namespace std; typedef long long LL;…
给定两个四位素数a  b,要求把a变换到b 变换的过程要保证  每次变换出来的数都是一个 四位素数,而且当前这步的变换所得的素数  与  前一步得到的素数  只能有一个位不同,而且每步得到的素数都不能重复. 求从a到b最少需要的变换次数.无法变换则输出Impossible   输入: 第一行 是一个数T 表示下面有T个测试数据 下面T行  每行两个数a b 解题思路:每次变换一次,个十百千,四个位置上的数每个从0-9循环一边一共需要4 * 10 = 40次循环  剪枝之后就没多少了 所以直接暴力…
Problem Description A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..., n into each circle separately, and the sum of numbers in two adjacent circles should be a prime. Note: the number of first circle should always be 1…