gcd和ex_gcd】的更多相关文章

gcd就是欧几里得算法,可以快速的求出俩个数的最大公因数,进而也可以求其最大公倍数(俩数之积除以最大公因数),比较简单直接看代码就好了,一般用递归版,简短精简,敲得快,但如果数剧奇葩,怕溢出,那就用递推版的. 递归版: int gcd(int a,int b) {   if(b==0) return a; return gcd(b,a%b); } 递推版: int gcd(int a,int b) {    int r=a%b while(r>0) {   a=b; b=r; r=a%b; }…
Lucky7 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5768 Description When ?? was born, seven crows flew in and stopped beside him. In its childhood, ?? had been unfortunately fall into the sea. While it was dying, seven dolphins arched its body an…
Mike and Geometry Problem 题目链接: http://acm.hust.edu.cn/vjudge/contest/121333#problem/I Description Mike wants to prepare for IMO but he doesn't know geometry, so his teacher gave him an interesting geometry problem. Let's define f([l, r]) = r - l + 1…
Ant Time Limit: 1 Second      Memory Limit: 32768 KB There is an ant named Alice. Alice likes going hiking very much. Today, she wants to climb a cuboid. The length of cuboid's longest edge is n, and the other edges are all positive integers. Alice's…
A Boring Question 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5793 Description Input The first line of the input contains the only integer T, Then T lines follow,the i-th line contains two integers n,m. Output For each n and m,output the answer i…
Two Arithmetic Progressions 题目链接: http://codeforces.com/contest/710/problem/D Description You are given two arithmetic progressions: a1k + b1 and a2l + b2. Find the number of integers x such that L ≤ x ≤ R and x = a1k' + b1 = a2l' + b2, for some inte…
~>>_<<~ 咳咳!!!今天写此笔记,以防他日老年痴呆后不会解方程了!!! Begin ! ~1~, 首先呢,就看到了一个 gcd(a,b),这是什么鬼玩意呢?什么鬼玩意并不重要,重要的她代表的含义,其实呢,gcd(a,b)就表示 非负整数 a 和 b(不同时为0) 的最大公约数,(数论概论上说:计算 a 与 b 的最大公因数的更低效方法是我女儿四年级老师教的方法,老师要求学生求出 a 与 b 的所有因数,然后找出同时出现在两个表中的最大数字. YES!A good idea f…
一些关于GCD的代码.... #include <iostream> #include <cstdio> #include <cstring> using namespace std; typedef long long int LL; LL EX_GCD(LL a,LL b,LL& x,LL& y) { ) { x=;y=; return a; } else { LL ret=EX_GCD(b,a%b,x,y); int t=x; x=y; y=t-a…
http://acm.hdu.edu.cn/showproblem.php?pid=5780 BC #85 1005 思路: 首先原式化简:x​^gcd(a,b)​​−1 也就是求n内,(公约数是i的对数)*x^i-1的和,其中i为n内的两两最大公约数.那么问题可以转化成先预处理出i,再求和,注意O(n*300)=1,正常情况会卡常数.必须还要优化 由于 ans=∑s[d]∗(x^​d​​−1),记s[d]=最大公约数为d的对数 我们注意到求s[d] or (公约数是i的对数),也就是求n/i以…
GCD int gcd(int a, int b) { return b == 0 ? a : gcd(b, a%b); } EXGCD void ex_gcd(int a, int b, int &x, int &y) { if (b == 0) { x = 1; y = 0; return; } else { ex_gcd(b, a%b, x, y); int t = x; x = y; y = t - (a / b)*y; } }…