题意:给出一个N,若N为素数,输出Prime.若为合数,输出最小的素因子.思路:Pollard rho大整数分解,模板题 #include <iostream> #include <stdio.h> #include <algorithm> #include <string.h> #include <cstdlib> #include <cmath> using namespace std; long long n; long lon…
题目传送门 题意:一个长为n(n<2^63)的管子,在管子上做标记,每隔L个长度单位做一个标记,从管子头端开始,保证最后一次标记恰好在管子的尾端.让你找出有多少个这样的L(L<n),且他们之间两两互素,然后求出这些L的和最大值. 分析:转换一下就是求n有多少个质因子用pollard_rho大整数分解分解n,因为素数之间两两互质,所以每段L都由每个质因子的k次幂组成,如果n是素数,由于L<n,所以只能L==1 收获:接触到随机算法 代码: /************************…
\(\\\) Miller-Rabin 素性测试 考虑如何检验一个数字是否为素数. 经典的试除法复杂度 \(O(\sqrt N)\) 适用于询问 \(N\le 10^{16}\) 的时候. 如果我们要把询问范围加到 \(10^{18}\) ,再多组询问呢? Miller 和 Rabin 建立了Miller-Rabin 质数测试算法. \(\\\) Fermat 测试 首先我们知道费马小定理: \[ a^{p-1}\equiv 1\pmod p \] 当且仅当 \(p\) 为素数时成立. 逆命题是…
整数分解,又称质因子分解.在数学中,整数分解问题是指:给出一个正整数,将其写成几个素数的乘积的形式. (每个合数都可以写成几个质数相乘的形式,这几个质数就都叫做这个合数的质因数.) .试除法(适用于范围比较小) 无论素数判定还是因子分解,试除法(Trial Division)都是首先要进行的步骤.令m=n,从2~根n一一枚举,如果当前数能够整除m,那么当前数就是n的素数因子,并用整数m 将当前数除尽为止. 若循环结束后m是大于1的整数,那么此时m也是n的素数因子. 事例如HDU1164:15mm…
BUPT2017 wintertraining(15) #8E 题意 长度为n(\(n<2^{63}\))的绳子,每隔长度L(1<L<n)做一次标记,标记值就是L,L是n的约数. 每轮标记都选一个L,且L之间两两互质. 求L的最多种数K.以及标记之和S的最大值. 题解 对n进行分解质因数,K就是不同质因子的个数,S就是p^{a_i}之和.不过题目要求L<n,所以当S算出来是n时,再除以一下最小的质因子. 分解比较小的n(<1e9),可以直接枚举,复杂度是\(O(\sqrt n…
链接:http://acm.hdu.edu.cn/showproblem.php? pid=3864 题意:给出一个数N(1<=N<10^18).假设N仅仅有四个约数.就输出除1外的三个约数. 思路:大数的质因数分解仅仅能用随机算法Miller Rabin和Pollard_rho.在測试多的情况下正确率是由保证的. 代码: #include <iostream> #include <cstdio> #include <cstring> #include &l…
问题描述:素性测试兼质因子分解 解题关键:pollard-rho质因数分解,在RSA的破译中也起到了很大的作用 期望复杂度:$O({n^{\frac{1}{4}}})$ #include<cstdio> #include<cstring> #include<algorithm> #include<cstdlib> #include<iostream> #include<cmath> #include<vector> #de…
#include <iostream> #include <cstring> #include <cstdlib> #include <stdio.h> #include <algorithm> #include <math.h> #include <stdlib.h> #include<time.h> #define ll long long #define INF 0x3f3f3f3f #define ma…
Given two positive integers a and b, we can easily calculate the greatest common divisor (GCD) and the least common multiple (LCM) of a and b. But what about the inverse? That is: given GCD and LCM, finding a and b. Input The input contains multiple…
进入a b 多少努力p, q 使p*q == a && p < q && p >= b 直接大整数分解 然后dfs所有可能的解决方案劫持 #include <cstdio> #include <cstring> #include <cstdlib> #include <algorithm> #include <cmath> using namespace std; typedef long long L…
链接:传送门 题意:输入 n ,判断 n 是否为素数,如果是合数输出 n 的最素因子 思路:Pollard-rho经典题 /************************************************************************* > File Name: Pollard_rho_Test.cpp > Author: WArobot > Blog: http://www.cnblogs.com/WArobot/ > Created Time:…
其实不过是大整数分解... 注意两点:注意L 不能==N 但是,N却可以是素数...囧 #include <iostream> #include <cstdio> #include <algorithm> #include <cstdio> #define LL __int64 #define MAX 1LL<<61 #define Times 8 #define N 501 #define C 101 using namespace std;…
题意: 考虑所有满足2 ≤ a ≤ 5和2 ≤ b ≤ 5的整数组合生成的幂ab: 22=4, 23=8, 24=16, 25=3232=9, 33=27, 34=81, 35=24342=16, 43=64, 44=256, 45=102452=25, 53=125, 54=625, 55=3125 如果把这些幂按照大小排列并去重,我们得到以下由15个不同的项组成的序列: 4, 8, 9, 16, 25, 27, 32, 64, 81, 125, 243, 256, 625, 1024, 31…
A + B Problem II Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B.    Input The first line of the inpu…
How Many Fibs? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 5954    Accepted Submission(s): 2331 Problem Description Recall the definition of the Fibonacci numbers: f1 := 1 f2 := 2 fn := fn-1…
输入整数(0-30)分解成所有整数之和.每四行换行一次. 一种方法是通过深度优先枚举出解.通过递归的方式来实现. #include <stdio.h> #include <string.h> #define MAXN 30 #define MAXString 64 int Terms[MAXN]; int N; int Count; void Search(int Remainder,int Start,int nTerm) { int i; ){ Count++; !=) pri…
★ 引子         前面两篇介绍了 Comba 乘法,最后提到当输入的规模很大时,所需的计算时间会急剧增长,因为 Comba 乘法的时间复杂度仍然是 O(n^2).想要打破乘法中 O(n^2) 的限制,需要从一个完全不同的角度来看待乘法.在下面的乘法算法中,需要使用 x 和 y 这两个大整数的多项式基表达式 f(x) 和 g(x) 来表示. 令 f(x) = a * x + b,g(x) = c * x + d,h(x) = f(x) * g(x).这里的 x 相当于一个基,比如十进制下,…
http://acm.hdu.edu.cn/showproblem.php?pid=1002 输入的数都是正整数,比较好处理,注意进位. //非负大整数加法 # include <stdio.h> # include <string.h> # define MAX 1100 int main() { int t; char Num1[MAX], Num2[MAX], Num3[MAX];//Num3[] 用于保存结果 scanf("%d", &t); f…
算法课有这么一节,专门介绍分治法的,上机实验课就是要代码实现大整数乘法.想当年比较混,没做出来,颇感遗憾,今天就把这债还了吧! 大整数乘法,就是乘法的两个乘数比较大,最后结果超过了整型甚至长整型的最大范围,此时如果需要得到精确结果,就不能常规的使用乘号直接计算了.没错,就需要采用分治的思想,将乘数“分割”,将大整数计算转换为小整数计算. 在这之前,让我们回忆一下小学学习乘法的场景吧.个位数乘法,是背诵乘法口诀表度过的,不提也罢:两位数乘法是怎么做的呢?现在就来一起回忆下12*34吧:    3 …
Prime Test Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 29193 Accepted: 7392 Case Time Limit: 4000MS Description Given a big integer number, you are required to find out whether it's a prime number. Input The first line contains the num…
最近在跟coursera上斯坦福大学的算法专项课,其中开篇提到了两个整数相乘的问题,其中最简单的方法就是模拟我们小学的整数乘法,可想而知这不是比较好的算法,这门课可以说非常棒,带领我们不断探索更优的算法,然后介绍可以通过使用分而治之的思想来解决这个问题.下面对该问题的方法以及实现进行介绍. 问题定义 输入:2个n位的整数x和y 输出:x * y 如求: 1234567891011121314151617181*2019181716151413121110987654 的结果 求解该问题要注意的是…
目录 问题 流程 代码 生日悖论 end 问题 给定n,要求对n质因数分解 普通的试除法已经不能应用于大整数了,我们需要更快的算法 流程 大概就是找出\(n=c*d\) 如果\(c\)是素数,结束,不是继续递归处理. 具体一点的话 1.先对n进行\(miller\_rabin\)测试,是素数就直接结束了 如果不会的话,看我前篇博客的介绍吧 为何还要多写个\(miller\_rabin\),他没有非平凡因子,他要保证复杂度? 2.随机基底a和c,生成序列\(x_{0}=a,x_{i}=x_{i-1…
GCD & LCM Inverse Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9756Accepted: 1819 Description Given two positive integers a and b, we can easily calculate the greatest common divisor (GCD) and the least common multiple (LCM) of a and b.…
★ 引子         前面两篇介绍了 Comba 乘法,最后提到当输入的规模很大时,所需的计算时间会急剧增长,因为 Comba 乘法的时间复杂度仍然是 O(n^2).想要打破乘法中 O(n^2) 的限制,需要从一个完全不同的角度来看待乘法.在下面的乘法算法中,需要使用 x 和 y 这两个大整数的多项式基表达式 f(x) 和 g(x) 来表示. 令 f(x) = a * x + b,g(x) = c * x + d,h(x) = f(x) * g(x).这里的 x 相当于一个基,比如十进制下,…
Prime Test Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 29193 Accepted: 7392 Case Time Limit: 4000MS Description Given a big integer number, you are required to find out whether it's a prime number. Input The first line contains the num…
一个k(1<=k<=80)位的十进制正整数N,就是所谓的大整数.请你设计程序,对于给出的某一个大整数N,找到满足p^3+p^2+3p<=n的p的最大值. 输入格式 输入数据只有一行,是一个K位的大整数N,行首行未无多余空格 输出格式 输出第一行为你所找到的P最大值,行首行末别乱加东西 样例1 样例输入1[复制]   1000000000000001000000000000003000000000000001 样例输出1[复制]   1000000000000000 def f(x): r…
Eddy's research I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6664    Accepted Submission(s): 3997 Problem Description Eddy's interest is very extensive, recently he is interested in prime…
啊居然要特判,卡了好久QAQ (好像Windows下的rand和Linux下的不一样? QwQ一些东西参考了喵铃的这篇blog:http://www.cnblogs.com/meowww/p/6400841.html  (业界良心) 题目 题意:输入$n$,求$phi(n)$,$n \leq 10^{18}$ 随便抽的题,刚好学习一下相关的算法. 很明显朴素的根号算法时间复杂度补滋兹,线性筛更不用想了,不过这题只需要单个欧拉函数值,还是直接考虑$phi(n)=n*\prod_{i=1}^k(1-…
题目链接 问题 A: 整数分解为 2 的幂 题目描述 任何正整数都能分解成 2 的幂,给定整数 N,求 N 的此类划分方法的数量!由于方案数量较大,输出 Mod 1000000007 的结果. 比如 N = 7 时,共有 6 种划分方法. 7=1+1+1+1+1+1+1 =1+1+1+1+1+2 =1+1+1+2+2 =1+2+2+2 =1+1+1+4 =1+2+4 输入 输入一个数 N(1≤N≤10^6) 输出 输出划分方法的数量 Mod 1000000007 样例输入 7 样例输出 6 先放…
一,题意: 大整数乘法模板题二,思路: 1,模拟乘法(注意"逢十进一") 2,倒序输出(注意首位0不输出) 三,步骤: 如:555 x 35 = 19425  5 5 5  5 5 5 x   3 5 x    3 5 -----------   ==>    ----------   2 7 7 5 25 25 25    + 1 6 6 5   +15 15 15 -------------  -----------------    1 9 4 2 5 15 40 40 2…