拓展欧几里得求 ax + by = c的通解(a >=0, b >= 0)
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector> using namespace std; #define ll long long // 题目:给定三种物品的价格A,B,C和拥有的钱P(C / gcd(A, B, C) >= 200)
// 求解 AX + BY + CZ = P的解个数(case = 100)
// A, B, C, P ∈ [0, 100000000] // 解:
// AX + BY = P - CZ (C >= 200 -> Z <= 1e8 / 200 = 5e5)
// 复杂度O(case * 5e5 * log(1e8)) const int INF = 1e9;
const ll MOD = ;
ll a, b, c, p, x, y, gcd_ab; ll exgcd(ll a, ll b, ll &x, ll &y)
{ if(b == ){//推理1,终止条件
x = ;
y = ;
return a;
}
ll r = exgcd(b, a%b, x, y);
//先得到更底层的x2,y2,再根据计算好的x2,y2计算x1,y1。
//推理2,递推关系
ll t = y;
y = x - (a/b) * y;
x = t;
return r;
} ll fun(ll n)
{
if(n % gcd_ab) return ;
ll tim = n / gcd_ab;
ll xx, yy;
// a * (tim * x) + b * (tim * y) = gcd(a, b) * tim = n;
xx = x * tim;
yy = y * tim;
ll k, k1, k2;
//a * (xx + F) + b * (yy + G) = n
// Fa + Gb = 0
// F = lcm(a, b) / a * t
// G = lcm(a, b) / g * t
k1 = b / gcd_ab;
k2 = a / gcd_ab; if(xx < ){
k = -(xx / k1) + (xx % k1 != );
xx += k * k1; yy -= k * k2;
}
if(yy < ){
k = -(yy / k2) + (yy % k2 != );
xx -= k * k1; yy += k * k2;
} //cout << "aa = " << aa << " bb = " << bb << endl;
ll x1, x2, y1, y2;
if(xx < || yy < ) return ;
k = xx / k1;
xx -= k * k1;
yy += k * k2;
x1 = xx, y1 = yy;
k = yy / k2;
xx += k * k1;
yy -= k * k2;
x2 = xx, y2 = yy;
y2 = y2 - k * k2;
//x1 + (x2 - x1) / k1 = x2
return (x2 - x1) / k1 + ;
} void solve()
{ int T, _case = ;
cin >> T;
while(T--){ cin >> a >> b >> c >> p;
//ax + by = gcd(a, b)
gcd_ab = exgcd(a, b, x, y);
//cout << x << " " << y << endl;
ll ways = ;
for(ll i = ; p - c * i >= ; ++i){
ways += fun(p - c * i);
//cout << "ways = " << ways << endl;
} cout << "Case " << ++_case << ": " << ways << endl;
}
} int main()
{ solve(); //cout << "ok" << endl; return ;
}
拓展欧几里得求 ax + by = c的通解(a >=0, b >= 0)的更多相关文章
- gcd模板(欧几里得与扩展欧几里得、拓展欧几里得求逆元)
gcd(欧几里得算法辗转相除法): gcd ( a , b )= d : 即 d = gcd ( a , b ) = gcd ( b , a mod b ):以此式进行递归即可. 之前一直愚蠢地以为辗 ...
- Modular Inverse (拓展欧几里得求逆元)
The modular modular multiplicative inverse of an integer a modulo m is an integer xsuch that a-1≡x ( ...
- 扩展欧几里得 求ax+by == n的非负整数解个数
求解形如ax+by == n (a,b已知)的方程的非负整数解个数时,需要用到扩展欧几里得定理,先求出最小的x的值,然后通过处理剩下的区间长度即可得到答案. 放出模板: ll gcd(ll a, ll ...
- POJ 1061 青蛙的约会(拓展欧几里得求同余方程,解ax+by=c)
青蛙的约会 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 122871 Accepted: 26147 Descript ...
- ZOJ 3593 One Person Game(拓展欧几里得求最小步数)
One Person Game Time Limit: 2 Seconds Memory Limit: 65536 KB There is an interesting and simple ...
- ZOJ 3609 Modular Inverse(拓展欧几里得求最小逆元)
Modular Inverse Time Limit: 2 Seconds Memory Limit: 65536 KB The modular modular multiplicative ...
- POJ 2891 Strange Way to Express Integers(拓展欧几里得)
Description Elina is reading a book written by Rujia Liu, which introduces a strange way to express ...
- Looooops(求解同余方程、同余方程用法)【拓展欧几里得】
Looooops(点击) A Compiler Mystery: We are given a C-language style for loop of type for (variable = A; ...
- 青蛙的约会 (ax+by=c求最小整数解)【拓展欧几里得】
青蛙的约会(点击跳转) 两只青蛙在网上相识了,它们聊得很开心,于是觉得很有必要见一面.它们很高兴地发现它们住 ...
随机推荐
- 数据可视化实例(十一): 矩阵图(matplotlib,pandas)
矩阵图 https://datawhalechina.github.io/pms50/#/chapter9/chapter9 导入所需要的库 import numpy as np # 导入numpy库 ...
- python之将一个字符串str的内容倒叙过来,并输出。
inStr = input() flashback = inStr[::-1] print(flashback)
- Threejs实现滴滴官网首页地球动画
.katex { display: block; text-align: center; white-space: nowrap; } .katex-display > .katex > ...
- js&jsp规范问题
1.js初始化问题 一般与数据库交互的需要进行初始化,固定控件一般不需要初始化.有些需要整体浏览器页面校准的可能需要初始化. //初始化操作按钮 $(function(){ ...
- Go Pentester - HTTP CLIENTS(1)
Building HTTP Clients that interact with a variety of security tools and resources. Basic Preparatio ...
- OSCP Learning Notes - Scanning(2)
Scanning with Metasploite: 1. Start the Metasploite using msfconsole 2. search modules 3.Choose one ...
- P1776 宝物筛选
题目: 正文: 啊,多重背包真恶心... 一开始我是把多重背包改成了01背包,然鹅我当时是直接1个1个的往后摞的... 参见以下代码: for(int i=1;i<=n;++i){//平平无奇的 ...
- 小谢第37问:关于websocket推送进度,本地保存进度条,然后跳出页面进入后再显示的问题
1.主要技术点:sessionStorage 会话存储进度 这里在使用之前,顺便说一下cookie.sessionStorage.localStorage 共同点:都是保存在浏览器端,且同源的. 区别 ...
- 设计模式:composite模式
目的:使容器和内容具备一致性 实现:将对象组合成树形结构以表示“部分-整体”的层次结构 实例:文件夹中可以包含文件夹也可以包含文件 例子: class Item //接口定义 { public: vi ...
- 10个Vue开发技巧助力成为更好的工程师·二
优雅更新props 更新 prop 在业务中是很常见的需求,但在子组件中不允许直接修改 prop,因为这种做法不符合单向数据流的原则,在开发模式下还会报出警告.因此大多数人会通过 $emit 触发自定 ...