数论-GCD、LCM、扩展欧几里得
文章目录
- 最大公约数GCD
- 最小公倍数LCM
- 扩展欧几里得
- 例题
- HDU-5223
- HDU-1576
最大公约数GCD
欧几里得算法(辗转相除法)求GCD
int gcd(int x, int y) {
return y == 0 ? x : gcd(y, x % y);
}
最小公倍数LCM
int lcm(int x, int y) {
return x / gcd(x, y) * y;
}
扩展欧几里得
ll extend_gcd(ll a, ll b, ll& x, ll& y) {
if (b == 0) {
x = 1; y = 0;
return a;
}
ll gcd = extend_gcd(b, a % b, y, x);
y -= x * (a / b);
return gcd;
}
- 求解不定方程
- 求解摸的逆元
- 求解同余方程
例题
HDU-5223
Problem Description In mathematics, the greatest common divisor (gcd) of two or more integers, when at least one of them is not zero, is the largest positive integer that divides the numbers without a remainder. For example, the GCD of 8 and 12 is 4.—Wikipedia BrotherK and Ery like playing mathematic games. Today, they are playing a game with GCD. BrotherK has an array A with N elements: A1 ~ AN, each element is a integer in [1, 10^9]. Ery has Q questions, the i-th question is to calculate GCD(ALi, ALi+1, ALi+2, …, ARi), and BrotherK will tell her the answer. BrotherK feels tired after he has answered Q questions, so Ery can only play with herself, but she don’t know any elements in array A. Fortunately, Ery remembered all her questions and BrotherK’s answer, now she wants to recovery the array A. Input The first line contains a single integer T, indicating the number of test cases. Each test case begins with two integers N, Q, indicating the number of array A, and the number of Ery’s questions. Following Q lines, each line contains three integers Li, Ri and Ansi, describing the question and BrotherK’s answer. T is about 10 2 ≤ N Q ≤ 1000 1 ≤ Li < Ri ≤ N 1 ≤ Ansi ≤ 109 Output For each test, print one line. If Ery can’t find any array satisfy all her question and BrotherK’s answer, print “Stupid BrotherK!” (without quotation marks). Otherwise, print N integer, i-th integer is Ai. If there are many solutions, you should print the one with minimal sum of elements. If there are still many solutions, print any of them. Sample Input 2 2 2 1 2 1 1 2 2 2 1 1 2 2 Sample Output Stupid BrotherK! 2 2
给定若干区间的GCD,试还原原数组。 贪心乘最小的数使得区间内每个数是ans[i]的倍数(LCM),最后再检查一遍。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1003;
ll t, n, q;
ll a[maxn], l[maxn], r[maxn], ans[maxn];
ll gcd(ll x, ll y) {
return y == 0 ? x : gcd(y, x % y);
}
ll lcm(ll x, ll y) {
return x / gcd(x, y) * y;
}
int main() {
cin >> t;
while (t--) {
cin >> n >> q;
for (int i = 1; i <= n; i++)a[i] = 1; //初始化原数组1
for (int i = 1; i <= q; i++) {
cin >> l[i] >> r[i] >> ans[i];
for (int j = l[i]; j <= r[i]; j++)
a[j] = lcm(a[j], ans[i]);
}
bool tag = true;
for (int i = 1; i <= q; i++) { //检验
ll tmp = a[l[i]];
for (ll j = l[i] + 1; j <= r[i]; j++)
tmp = gcd(tmp, a[j]);
if (tmp != ans[i]) {
tag = false;
break;
}
}
if (tag) {
cout << a[1];
for (int i = 2; i <= n; i++)cout << " " << a[i];
cout << "n";
}
else cout << "Stupid BrotherK!n";
}
return 0;
}
HDU-1576
Problem Description 要求(A/B)%9973,但由于A很大,我们只给出n(n=A%9973)(我们给定的A必能被B整除,且gcd(B,9973) = 1)。 Input 数据的第一行是一个T,表示有T组数据。 每组数据有两个数n(0 <= n < 9973)和B(1 <= B <= 10^9)。 Output 对应每组数据输出(A/B)%9973。 Sample Input 2 1000 53 87 123456789 Sample Output 7922 6060
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define mod 9973
ll t, n, a, b, x, y;
ll extend_gcd(ll a, ll b, ll& x, ll& y) {
if (b == 0) {
x = 1; y = 0;
return a;
}
ll gcd = extend_gcd(b, a % b, y, x);
y -= x * (a / b);
return gcd;
}
int main() {
cin >> t;
while (t--) {
cin >> n >> b;
extend_gcd(b, mod, x, y);
x *= n;
x = (x % mod + mod) % mod;
cout << x << "n";
}
return 0;
}
原创不易,请勿转载(
本不富裕的访问量雪上加霜) 博主首页:https://blog.csdn.net/qq_45034708
- 程序员必知之SEO
- 进程监控工具supervisor 启动Mongodb
- 祭奠那些年,我弃坑的开源轮子
- 这些奇技浮巧,助你优化前端应用性能
- Stepping.js——两步完成前后端分离架构设计
- 我的职业是前端工程师【十】客户端存储艺术:数据存储与模型
- 【开源】2md:将复制的内容、网页转成 markdown
- React Native 持续部署实践— push 代码构建出新版的 Growth
- 技巧 - 如何好一个 Git 提交信息及几种不同的规范
- React、Vue、Ember 及其他前端开发者,请暂缓更新到 Chrome 59 浏览器
- 微软开源全新的文档生成工具DocFX
- 使用 MimeKit 和 MailKit 发送邮件
- 使用 React Native 重写大型 Ionic 应用后,我们想分享一下这八个经验
- 基于OWin的Web服务器Katana发布版本3
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- Android实现扫一扫识别数字功能
- 通过SSH连接本地linux虚拟机的过程记录
- 实现Android 获取cache缓存的目录路径的方法
- Android 消息分发使用EventBus的实例详解
- 详解Android实现定时器的几种方法
- Android 实现带进度条的WebView的实例
- Android单元测试之对Activity的测试示例
- Android开发之手势检测及通过手势实现翻页功能的方法
- Android开发之CheckBox的简单使用与监听功能示例
- Android将图片上传到php服务器的实例代码
- Android图片识别应用详解
- Android 列表形式的切换的示例代码
- Android 拍照并对照片进行裁剪和压缩实例详解
- Android 多线程实现重复启动与停止的服务
- Android 简单的图片查看器源码实现