拓展欧几里得求 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求最小整数解)【拓展欧几里得】
青蛙的约会(点击跳转) 两只青蛙在网上相识了,它们聊得很开心,于是觉得很有必要见一面.它们很高兴地发现它们住 ...
随机推荐
- SpringBoot学习笔记(十七:异步调用)
@ 目录 1.@EnableAsync 2.@Async 2.1.无返回值的异步方法 2.1.有返回值的异步方法 3. Executor 3.1.方法级别重写Executor 3.2.应用级别重写Ex ...
- oop的三种设计模式(单例、工厂、策略)
参考网站 单例模式: 废话不多说,我们直接上代码: <?php /** 三私一公 *私有的静态属性:保存类的单例 *私有的__construct():阻止在类的外部实例化 *私有的__clone ...
- java的干儿子锁Lock
目录 Lock的由来 线程之间的交互 Lock方法简介 lock() lockInterruptibly() trylock() trylock(long,TimeUnit) unlock() new ...
- Go Pentester - HTTP CLIENTS(4)
Interacting with Metasploit msf.go package rpc import ( "bytes" "fmt" "gopk ...
- CSS栅格布局
CSS栅格布局 认识栅格布局 CSS的栅格布局也被称为网格布局(Grid Layout),它是一种新兴的布局方式. 栅格布局是一个二维系统,这意味着它可以同时处理列和行,与弹性布局相似,栅格系统也是由 ...
- C++语法小记---string类
string类 #include <iostream> #include <string> using namespace std; // 实现字符串右移, 例子hello & ...
- 一张PDF了解JDK11 GC调优秘籍-附PDF下载
目录 简介 废弃的VM选项 Source-File Mode Code Heap状态分析 AppCDS 总结 简介 JDK11相比JDK10,添加了一个新的Source-File Mode,可以直接通 ...
- JELLY技术周刊 Vol.15 云游戏会是 5G 杀手级应用么?
蒲公英 · JELLY技术周刊 Vol.15 听到"云游戏",或许我们的第一反应会是"云玩家"而不是那些上云的"游戏",在这个 5G 已来的 ...
- 解决android studio Gradle无法同步问题
打开根目录build.gradle buildscript { repositories { // 添加阿里云 maven 地址 maven { url 'http://maven.aliyun.co ...
- three.js 数学方法之Matrix3
今天郭先生来说一说three.js的三维矩阵,这块知识需要结合线性代数的一些知识,毕业时间有点长,线性代数的知识大部分都还给了老师.于是一起简单的复习了一下.所有的计算都是使用列优先顺序进行的.然而, ...