Description 众所周知,我是好人!所以不会出太难的题,题意很简单 给你两个数n和m,问你有多少对正整数对最大公约数是n,最小公倍数是m最后友情提供解题代码(我真是太好人了) void solve() { long long n, m; scanf("%lld%lld", &n, &m); ; ; i <= m; i++) { for (long long j = i; j <= m; j++) { if (gcd(i, j) == n &&…
数论入门2 另一种类型的数论... GCD,LCM 定义\(gcd(a,b)\)为a和b的最大公约数,\(lcm(a,b)\)为a和b的最小公倍数,则有: 将a和b分解质因数为\(a=p1^{a1}p2^{a2}p3^{a3}...pn^{an},b=p1^{b1}p2^{b2}p3^{b3}...pn^{bn}\),那么\(gcd(a,b)=\prod_{i=1}^{n}pi^{min(ai,bi)},lcm(a,b)=\prod_{i=1}^{n}pi^{max(ai,bi)}\)(0和任何…
Problem F: 我是好人4 Description 众所周知,我是好人!所以不会出太难的题,题意很简单 给你n个数,问你1000000000(含1e9)以内有多少个正整数不是这n个数任意一个的倍数 最后友情提供解题代码(我真是太好人了) void solve(int p[], int n) { int ans = 0; for (int i = 1; i <= 1e9; i++) { int fl = 0; for (int j = 0; j < n; j++) { if (i % p[…
GCD and LCM HDU 4497 数论 题意 给你三个数x,y,z的最大公约数G和最小公倍数L,问你三个数字一共有几种可能.注意123和321算两种情况. 解题思路 L代表LCM,G代表GCD. \[ x=(p_1^{i_1})*(p_2^{i_2})*(p_3^{i_3})\dots \] \[ y=(p_1^{j_1})*(p_2^{j_2})*(p_3^{j_3})\dots \] \[ z=(p_1^{k_1})*(p_2^{k_2})*(p_3^{k_3})\dots \] \…
GCD?LCM! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 316    Accepted Submission(s): 200 Output T lines, find S(n) mod 258280327. Sample Input 8 1 2 3 4 10 100 233 11037 Sample Output 1 5 1…
gcd(a, b),就是求a和b的最大公约数 lcm(a, b),就是求a和b的最小公倍数 然后有个公式 a*b = gcd * lcm     ( gcd就是gcd(a, b), ( •̀∀•́ ) 简写你懂吗) 解释(不想看就跳过){ 首先,求一个gcd,然后... a / gcd 和 b / gcd 这两个数互质了,也就是 gcd(   a / gcd ,b / gcd  )  =  1,然后... lcm = gcd *  (a / gcd) * (b / gcd) lcm = (a *…
Problem F: 我是好人4 Description 众所周知,我是好人!所以不会出太难的题,题意很简单 给你n个数,问你1000000000(含1e9)以内有多少个正整数不是这n个数任意一个的倍数 最后友情提供解题代码(我真是太好人了) void solve(int p[], int n) { int ans = 0; for (int i = 1; i <= 1e9; i++) { int fl = 0; for (int j = 0; j < n; j++) { if (i % p[…
根据最大公约数和最小公倍数求原来的两个数 题目大意,不翻译了,就是上面链接的意思. 具体思路就是要根据数论来,设a和b的GCD(最大公约数)和LCM(最小公倍数),则a/GCD*b/GCD=LCM/GCD,我们只用枚举LCM/GCD的所有质因数就可以了,然后把相应的质因数乘以GCD即可得出答案. 找素数很简单,用Miller_Rabin求素数的方法,可以多求几次提高正确率,原理就是用的费马定理:如果P是素数,则A^(p-1)mod P恒等于1,为了绕过Carmichael数,采用费马小定理:如果…
题意:给出a和b的gcd和lcm,让你求a和b.按升序输出a和b.若有多组满足条件的a和b,那么输出a+b最小的.思路:lcm=a*b/gcd   lcm/gcd=a/gcd*b/gcd 可知a/gcd与b/gcd互质,由此我们可以先用Pollard_rho法对lcm/gcd进行整数分解, 然后对其因子进行深搜找出符合条件的两个互质的因数,然后再都乘以gcd即为输出答案. #include <iostream> #include <stdio.h> #include <alg…
UVA11388 GCD LCM Description of the title PDF The GCD of two positive integers is the largest integer that divides both the integers without any remainder. The LCM of two positive integers is the smallest positive integer that is divisible by both th…
GCD & LCM Inverse Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10621   Accepted: 1939 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…
[题目链接] http://poj.org/problem?id=2429 [题目大意] 给出最大公约数和最小公倍数,满足要求的x和y,且x+y最小 [题解] 我们发现,(x/gcd)*(y/gcd)=lcm/gcd,并且x/gcd和y/gcd互质 那么我们先利用把所有的质数求出来Pollard_Rho,将相同的质数合并 现在的问题转变成把合并后的质数分为两堆,使得x+y最小 我们考虑不等式a+b>=2sqrt(ab),在a趋向于sqrt(ab)的时候a+b越小 所以我们通过搜索求出最逼近sqr…
II U C   ONLINE   C ON TEST  Problem D: GCD LCM Input: standard input Output: standard output The GCD of two positive integers is the largest integer that divides both the integers without any remainder. The LCM of two positive integers is the smalle…
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3071 题目大意: 给定一个长度为n的序列m次操作,操作的种类一共有三种 查询 L :查询一个区间的所有的数的最小公倍数modp G :查询一个区间的所有的数的最大公约数modp 修改 C :将给定位置的值修改成x 解题思路: 注意数据范围,每个数字不超过100,所以100以内的质因子最多25个,如果直接求解lcm和gcd的话,long long也是存不下的,所以采用存储质因子的指数,但是如果每个节…
Description: [ 着实比较羞愧,都想着去暴力,把算法(方法)也忘了] A只涂x,2x,3x……,B只涂y,2y,3y……问你A和B共同涂的墙的个数 Solution: 就是求x和y的lcm,这里倒是想到了用x * y = gcd * lcm,但是算区间个数的时候我竟然去暴力了!!!! 区间1 - b所有的出现的个数是 b / lcm 区间1 - a所有的出现的个数是a / lcm 两个一相减就是(a,b]的个数了 很明显我们把a舍去了,最后可以加个判断也可以是b / lcm - (a…
UVA.12716 GCD XOR (暴力枚举 数论GCD) 题意分析 题意比较简单,求[1,n]范围内的整数队a,b(a<=b)的个数,使得 gcd(a,b) = a XOR b. 前置技能 XOR的性质 GCD 由于题目只给出一个n,我们要求对数,能做的也始终暴力枚举a,b,这样就有n^2的复杂度,由于n很大,根本过不了. 于是我们就想用到其中一些性质,如XOR 与GCD,不妨假设 a xor b = c,并且根据题意还知道, gcd(a,b) = c,也就说明c一定是a的因子,所以在枚举的…
(链接点这儿) 题目: The GCD of two positive integers is the largest integer that divides both the integers without any remainder. The LCM of two positive integers is the smallest positive integer that is divisible by both the integers. A positive integer can…
A /*Huyyt*/ #include<bits/stdc++.h> #define mem(a,b) memset(a,b,sizeof(a)) using namespace std; typedef long long ll; typedef unsigned long long ull; ][] = {{, }, {, }, {, -}, { -, }, {, }, {, -}, { -, -}, { -, }}; , gakki = + + + + 1e9; , MAXM = 2e…
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…
一.题目  Gcd & Lcm game 二.分析 非常好的一题. 首先考虑比较暴力的做法,肯定要按区间进行处理,对于$lcm$和$gcd$可以用标准的公式进行求,但是求$lcm$的时候是肯定会爆$long long$的. 考虑用素数分解,将所有的数分解后,发现素因子的个数有限,且每个因子的幂也有限,最多的也就是$2^_6$,然后可以考虑将素因子用二进制的每一位进行表示.对于$2,3,5,7$可能会要的多点,所以多给给几位就可以了,最后发现,刚好可以$32$位以内. 这里就需要写两个转换函数,然…
题目链接:http://gdutcode.sinaapp.com/problem.php?cid=1031&pid=5 这个题目一看就是一道数论题,应该考虑使用容斥原理,这里对lcm进行容斥. 不过直接上去是T,考虑到序列中同时存在i和ki的话,其实只需要考虑i,所以先对序列中为倍数的对进行处理. 这里的容斥用了hqw的写法. 代码: #include <iostream> #include <cstdio> #include <cstdlib> #includ…
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5382 题意:函数lcm(a,b):求两整数a,b的最小公倍数:函数gcd(a,b):求两整数a,b的最大公约数.函数[exp],其中exp是一个逻辑表达式.如果逻辑表达式exp是真,那么函数[exp]的值是1,否则函数[exp]的值是0.例如:[1+2>=3] = 1 ,[1+2>=4] = 0. 求S(n)的值. #include <bits/stdc++.h> using name…
A frog has just learned some number theory, and can't wait to show his ability to his girlfriend. Now the frog is sitting on a grid map of infinite rows and columns. Rows are numbered 1,2,⋯from the bottom, so are the columns. At first the frog is sit…
题目链接. 题意: 给定两个数,一个G,一个L,找出两个数a,b(a<=b),使得这两个数的最大公约数为G,最小公倍数为L,且(a最小). 分析: 当a,b存在时,a一定为G. 自己证了一下,数学方面不太擅长. 假设 a 最小为 k1G (其中 k1 != 1), b为 k2G, 即 a = G,不满足条件. 那么a*b=k1*k2*G^2=L*G 这时一定有 a1 = G, b2 = k1*k2G 满足条件.即a不符合题意. 设 a = G, b = kG 因为 L*G = a*b b = L…
决赛,我自我认为题目难度更大,反而我的心态更好了. 由于放轻松的时候反而效果更好,跟昨天的观点一样,凡是可以1A的,才算这题做得好. A.数目不大,关键是看懂题(我自己连输入输出是什么都不清楚.... 然后管理员就把题下掉了. . ..批评批评啊. .. )bin神的代码膜拜了下.知道是状态压缩,题中说了最多6个,要么1<<6枚举,要么6!枚举.代码: /* *********************************************** Author :kuangbin Cre…
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5584 给一个坐标(ex, ey),问是由哪几个点走过来的.走的规则是x或者y加上他们的最小公倍数lcm(x, y). 考虑(ex, ey)是由其他点走过来的,不妨设当走到(x,y)时候,gcd(x, y)=k,x=k*m1, y=k*m2. 下一步有可能是(x, y+x*y/gcd(x, y))或者是(x+x*y/gcd(x,y), y). 用k和m1,m2来表示为(k*m1, k*m2+m1*m2…
我一直相信这道题有十分巧妙的解法的,去搜了好多题解发现有的太过玄妙不能领会. 最简单的就是枚举n的所有约数,然后二重循环找lcm(a, b) = n的个数 #include <cstdio> #include <vector> #include <algorithm> using namespace std; ? a : gcd(b, a % b); } int lcm(int a, int b) { return a / gcd(a, b) * b; } int ma…
刚开始看这个题目,觉得没法做.关键点是数据小于100.因此,可以枚举所有小于100的素因子进行位压缩.gcd就是求最小值,lcm就是求最大值.c++有时候超时,g++800ms.线段树可解. /* 3071 */ #include <iostream> #include <sstream> #include <string> #include <map> #include <queue> #include <set> #include…
题目大意 给定两个数a,b的GCD和LCM,要求你求出a+b最小的a,b 题解 GCD(a,b)=G GCD(a/G,b/G)=1 LCM(a/G,b/G)=a/G*b/G=a*b/G^2=L/G 这样的话我们只要对L/G进行质因数分解,找出最接近√(L/G)的因子p,最终结果就是a=p*G,b=L/p,对(L/G)就是套用Miller–Rabin和Pollard's rho了,刚开始Pollard's rho用的函数也是 f(x)=x^2+1,然后死循环了....改成f(x)=x^2+c(c<…
先介绍两个: 大数的Gcd Stein+欧几里德 function stein(a,b:int64):int64; begin if a<b then exit(stein(b,a)); then exit(a); )=) )=) ,b>>)<<); )= ,b)); )= )); exit(stein((a+b)>>,(a-b)>>)); end; 小数的Gcd 辗转相除法 function stein(a,b:int64):int64; begin…