385C - Bear and Prime Numbers 思路:记录数组中1-1e7中每个数出现的次数,然后用素数筛看哪些能被素数整除,并加到记录该素数的数组中,然后1-1e7求一遍前缀和. 代码: #include<bits/stdc++.h> using namespace std; #define ll long long #define pb push_back #define mem(a,b) memset((a),(b),sizeof(a)) const int INF=0x3f…
原题链接 写个素数筛暴力打表一波就AC了: #include <iostream> using namespace std; const int N = 10001; int i, j, NotPrime[N]; long long sum[N]; void GetNotPrime() { for( i=2; i<=N; i++ ) if( !NotPrime[i] ) for( j = i+i; j<=N; j+=i ) NotPrime[j] = 1; } int k; voi…
题目链接:https://cn.vjudge.net/problem/LightOJ-1259 题意 给一个整数n,问有多少对素数a和b,使得a+b=n 思路 素数筛 埃氏筛O(nloglogn),这个完全够用,当n=3.5e7时将近一秒(1e8次操作) 欧拉筛O(n) 考虑数论专题过完了就写个模版专题 提交过程 AC 代码 #include <cstdio> #include <cstring> using namespace std; const int maxn=1e7+20…
Description The branch of mathematics called number theory is about properties of numbers. One of the areas that has captured the interest of number theoreticians for thousands of years is the question of primality. A prime number is a number that is…
题目链接 题意 : 就是把n个数安排在环上,要求每两个相邻的数之和一定是素数,第一个数一定是1.输出所有可能的排列. 思路 : 先打个素数表.然后循环去搜..... #include <cstdio> #include <cstring> #include <iostream> using namespace std ; ]; ] ,cs[]; int n ; void get_prime() { memset(prime,,sizeof(prime)); prime[…
题目链接:https://vjudge.net/problem/LightOJ-1370 题意:给你N个欧拉函数值,找出每一个大于等于该欧拉函数值的数,并且要求相加和最小. 题解:因为素数i的欧拉函数值等于i-1,所以我们只要找出大于等于i+1的素数即可. 1 #include<iostream> 2 #include<algorithm> 3 #include<vector> 4 #include<cstring> 5 #include<cstdio…
/* * 二次筛素数 * POJ268----Prime Distance(数论,素数筛) */ #include<cstdio> #include<vector> using namespace std; const int maxn = 1000005; typedef long long LL; bool is_prime_small[maxn]; bool is_prime[maxn]; vector <int> res; int main() { LL l,u…
题目链接: A. Mashmokh and Numbers time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output It's holiday. Mashmokh and his boss, Bimokh, are playing a game invented by Mashmokh. In this game Mashmokh wri…
The branch of mathematics called number theory is about properties of numbers. One of the areas that has captured the interest of number theoreticians for thousands of years is the question of primality. A prime number is a number that is has no prop…
Description The ministers of the cabinet were quite upset by the message from the Chief of Security stating that they would all have to change the four-digit room numbers on their offices. — It is a matter of security to change such things every now…
埃拉托斯特尼筛法(sieve of Eratosthenes ) 是古希腊数学家埃拉托斯特尼发明的计算素数的方法.对于求解不大于n的所有素数,我们先找出sqrt(n)内的所有素数p1到pk,其中k = sqrt(n),依次剔除Pi的倍数,剩下的所有数都是素数. 具体操作如上述 图片所示. C++实现 #include<iostream> #include<vector> using namespace std; int main() { int n; cin >> n;…
题目链接: C. Primes or Palindromes? time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standard output Rikhail Mubinchik believes that the current definition of prime numbers is obsolete as they are too complex…
abs 题意: 问题描述 给定一个数x,求正整数y,使得满足以下条件: 1.y-x的绝对值最小 2.y的质因数分解式中每个质因数均恰好出现2次. 输入描述 第一行输入一个整数T 每组数据有一行,一个整数x 输出描述 对于每组数据,输出一行y-x的最小绝对值 输入样例 5 1112 4290 8716 9957 9095 输出样例 23 65 67 244 70 题解: 由于y质因数分解式中每个质因数均出现2次,那么y是一个完全平方数,设y=z*z,题目可转换成求z,使得每个质因数出现1次. 我们…
这题就是先写个素数筛,存到prime里,之后遍历就好,取余,看是否等于0,如果等于0就更新,感觉自己说的不明白,引用下别人的话吧: 素数打表,找出20000之前的所有素数,存入prime数组,对于每个输入的数a,从prime数组最后一个数往前判断,如果a % prime[当前序号]== 0,那么将该素数就是输入数据a的最大素因子,找出所有输入数据的最大素因子,找出最大的那个对应的数据a即可,当a == 1的时候要特别处理. 然而我现在还是不懂,为什么输入是1 1的时候输出1,题目也没说啊,还有最…
题目链接:http://codeforces.com/problemset/problem/449/C 给你n个数,从1到n.然后从这些数中挑选出不互质的数对最多有多少对. 先是素数筛,显然2的倍数的个数是最多的,所以最后处理.然后处理3,5,7,11...的倍数的数,之前已经挑过的就不能再选了.要是一个素数p的倍数个数是奇数,就把2*p给2 的倍数.这样可以满足p倍数搭配的对数是最优的.最后处理2的倍数就行了. #include <bits/stdc++.h> using namespace…
折腾了一晚上很水的数论,整个人都萌萌哒 主要看了欧拉筛和素数筛的O(n)的算法 这个比那个一长串英文名的算法的优势在于没有多次计算一个数,也就是说一个数只筛了一次,主要是在%==0之后跳出实现的,具体的解释看的迷迷糊糊,特别是欧拉函数的求解 http://blog.csdn.net/lerenceray/article/details/12420725 代码如下 void ES(){ ;i<n;i++){ if (!pd[i]){ prime[++top]=i; phi[i]=i-; } ;j<…
Some positive integers can be represented by a sum of one or more consecutive prime numbers. How many such representations does a given positive integer have? For example, the integer 53 has two representations 5 + 7 + 11 + 13 + 17 and 53. The intege…
题目链接 题意:对2~n+1染色,一个数不能与其素因子同色. 故而只需两种颜色即可,素数染1,合数染2便可满足条件 #include<bits/stdc++.h> using namespace std; typedef long long LL; //素数筛打表 ; ],num_prime=; ]; void init() { isNotPrime[]=isNotPrime[]=; ; i<=N; i++) { if(!isNotPrime[i]) prime[num_prime++]…
ACM数论——素数  素数定义: 质数(prime number)又称素数,有无限个.质数定义为在大于1的自然数中,除了1和它本身以外不再有其他因数,这样的数称为质数.例 子:2.3.5.7.11.13.17.19.(那时候还有一种说法叫做“质数”,但是就语言上来说,我觉得“素数”这种叫法和“合数”比较搭配,类比于“化学元素”和“化合物”来看,叫“素数”非常贴切) 素数一些性质: 质数p的约数只有两个:1和p: 任一大于1的自然数,要么本身是质数,要么可以分解为几个质数之积,这种分解是唯一的:…
哥德巴赫猜想 Time Limit: 2000/1000ms (Java/Others) Problem Description: 哥德巴赫猜想大概是这么一回事:“偶数(>=4) == 两个质数的和”,至于是不是.成不成立,随它吧. Input: 多组数据,每组一个偶数n(4<=n<=10^6) Output: 若能分解则输出这两个质数,若有多解则按小数优先规则输出全部解,若无解则输出"No"; Sample Input: 16 Sample Output: 3 13…
传送门:http://codeforces.com/contest/1047/problem/C 题意: 给定n个数,问最少要去掉几个数,使得剩下的数gcd 大于原来n个数的gcd值. 思路: 自己一开始想把每个数的因子都找出来,找到这些因子中出现次数最多且因子大于n个数的最大公约数的,(n - 次数 )就是答案.但是复杂度是1e9,差那么一点. 自己还是对素数筛理解的不够深.这道题可以枚举素数x,对于每个x,找到所有(a[i]/gcd(all)) 是x倍数的个数,就是一个次数.找这个次数的过程…
题目描述: Soldier and Number Game time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standard output Two soldiers are playing a game. At the beginning first of them chooses a positive integer n and gives it to t…
Problem:找出小于等于n的所有素数的个数. #include <bits/stdc++.h> using namespace std; const int maxn = 1e6; int prime[maxn]; // 欧拉线性素数筛,O(n) bool vis[maxn]; // 标记 int Prime(int n) { memset(vis,false,sizeof(vis)); int cnt = 0; vis[0] = vis[1] = true; for(int i = 2;…
题意 删去最少的数,使gcd变大 题解 只要保留相同素数因子最多的数即可. 素数筛. C++代码 #include<bits/stdc++.h> using namespace std; ; ; int Mark[MAXN]; int prime[MAXN]; int Prime(){ ; memset(Mark,,sizeof(Mark)); Mark[] = ; Mark[] = ; ; i < MAXN; i++){ ){ prime[tot++] = i; } ; j <…
素数筛:需要一个数组进行标记 最小的素数2,所有是2的倍数的数都是合数,对合数进行标记,然后找大于2的第一个非标记的数(肯定是素数),将其倍数进行标记,如此反复,若是找n以内的所有素数,只需要对[2,n^0.5]进行循环即可,因为n以内的所有数如果不是[2,n^0.5]的倍数,则一定是素数.复杂度:O(nloglogn); for(int i=2;i*i<=n;i++){ if(a[i]!=-1) for(int k=i*i;k<=n;k+=i) a[i]=-1;} poj3126题目链接:h…
传送门:http://codeforces.com/contest/1047/problem/C 题意:给出n个数字,求最少删除几个数可以使剩下的数字的GCD大于n个数字的GCD 思路:最开始想的是先遍历求N个数的GCD,然后外层循环从i=g+1(g=GCD)开始,内层从j=1开始找a[j]%i==0的个数t, 求出最大的个数,如果求出的个数不为零,输出n-t,否则输出-1,但这样会超时,要优化: 看了别人的题解,要用到素数筛的方法. 第一步都是遍历求g=GCD.第二步求a[i]/g出现的次数.…
Help Hanzo 题意:求a~b间素数个数(1 ≤ a ≤ b < 231, b - a ≤ 100000).     (全题在文末) 题解: a~b枚举必定TLE,普通打表MLE,真是头疼.. b - a ≤ 100000 是关键. 类似素数筛的方法: 1.初始化vis[]=0 ; 2.素数的倍数vis[]=1; 3.  b较小时,素数筛解决   b很大时,素数筛的vis[]会MLE,此时用vis2[i-a]保存vis[i]就不会MLE 了.. #include<iostream>…
素数筛 #include<stdio.h> #include<string.h> #include<algorithm> using namespace std; #define MAXN 47000 #define inf 100000000 bool z[MAXN]; int x[MAXN]; ]; ]; int main() { int a,b; ;i<=;i++) //可以从小的素数开始筛 { if(!z[i]) for(int j=i*i;j<MA…
题目链接:http://lightoj.com/volume_showproblem.php?problem=1197 给你a和b求a到b之间的素数个数. 先在小区间素数筛,大区间就用类似素数筛的想法,把a到b之间不是素数的标记出来.因为b-a最多1e5的大小,所以每组数据的时间复杂度最多就o(1e5 log1e5). #include <iostream> #include <cstdio> #include <cstring> using namespace std…
第六周H题 - 数论,晒素数 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u   Description 小明对数的研究比较热爱,一谈到数,脑子里就涌现出好多数的问题,今天,小明想考考你对素数的认识.  问题是这样的:一个十进制数,如果是素数,而且它的各位数字和也是素数,则称之为“美素数”,如29,本身是素数,而且2+9 = 11也是素数,所以它是美素数.  给定一个区间,你能计算出这个区间…