拓展欧几里得求 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求最小整数解)【拓展欧几里得】
青蛙的约会(点击跳转) 两只青蛙在网上相识了,它们聊得很开心,于是觉得很有必要见一面.它们很高兴地发现它们住 ...
随机推荐
- hihoCoder 1051 补提交卡 最详细的解题报告
题目来源:补提交卡 解题思路:假设未提交程序的天数为:a1,a2,....,an,补交的张数为M.依次从a1,a2,....,an中去掉连续的 K 天(0<=K<=M),然后再来计算剩余数 ...
- Configurate vim tool
vim tool is a commom editor, for the sake of improving effeicient, it is necessary to configurate vi ...
- html 转义和反转义
public static void main(String[] args) {// String html = "<img style=\"width: 100%; hei ...
- oracle 启动报ORA-01105 ORA-19808
bash-4.4$ srvctl start instance -i jfcddb2 -d jfcddb PRCR-1013 : Failed to start resource ora.jfcddb ...
- BT面板安装教程
面板特点 一键配置服务器环境(LAMP/LNMP) 一键安全重启 一键创建管理网站.ftp.数据库 一键配置(定期备份.数据导入.伪静态.301.SSL.子目录.反向代理.切换PHP版本) 一键安装常 ...
- DirectX11 With Windows SDK--35 粒子系统
前言 在这一章中,我们主要关注的是如何模拟一系列粒子,并控制它们运动.这些粒子的行为都是类似的,但它们也带有一定的随机性.这一堆粒子的几何我们叫它为粒子系统,它可以被用于模拟一些比较现象,如:火焰.雨 ...
- 有关 Session 的那些事儿
原文链接: https://blog.by24.cn/archives/about-session.html Web 开发中,Session 是经常用到的概念,但是在日常交流中,似乎又经常引起误解. ...
- Java继承多态
一. 父类引用指向子类对象父类名(接口名) 对象名=new 子类(接口)名称() 二. 父子继承关系中,成员变量重名1.直接通过子类对象访问成员变量 =左边是谁,就优先用谁2.间接通过成员方法访问成员 ...
- js 绑定的键盘事件
在全局绑定键盘事件 document.onkeydown = function(event){ //在全局中绑定按下事件 var e = event || window.e; va ...
- jmeter接口测试 -- status==400(Bad Request)
一.接口请求信息 二.错误的jmeter接口请求 1.请求内容 2.响应内容 三.正确的接口请求 1.看回原本的接口请求信息,company_id = null .这里也就不能空 四.原因分析 1. ...