POJ 2142 The Balance(exgcd)

嗯...

题目链接:http://poj.org/problem?id=2142

AC代码:

 1 #include<cstdio>
 2 #include<iostream>
 3
 4 using namespace std;
 5
 6 inline int _abs(int x){
 7     if(x < 0) return -x;
 8     return x;
 9 }
10
11 inline void exgcd(int a, int b, int &g, int &x, int &y){
12     if(!b) { g = a; x = 1; y = 0;}
13     else { exgcd(b, a % b, g, y, x); y -= x * (a / b);}
14 }
15
16 inline void work(int a, int b, int c, int &g, int &x, int &y){
17     exgcd(a, b, g, x, y);
18     x *= c / g;
19     int t = b / g;
20     x = (x % t + t) % t;
21     y = _abs((a * x - c) / b);
22 }
23
24 int main(){
25     int a, b, c, x1, g, y1, x2, y2;
26     while(~scanf("%d%d%d", &a, &b, &c)){
27         if(!a && !b && !c) break;
28         work(a, b, c, g, x1, y1);
29         work(b, a, c, g, x2, y2);
30         if(x1 + y1 < x2 + y2) printf("%d %d\n", x1, y1);
31         else printf("%d %d\n", y2, x2);
32     }
33     return 0;
34 }

AC代码

原文地址:https://www.cnblogs.com/New-ljx/p/11515354.html

时间: 2024-08-28 05:25:45

POJ 2142 The Balance(exgcd)的相关文章

POJ 2115 C Looooops(Exgcd)

[题目链接] http://poj.org/problem?id=2115 [题目大意] 求for (variable = A; variable != B; variable += C)的循环次数, 其中变量为k比特无符号整数. [题解] 题目等价于求解Cx=(B–A)(mod 2^k),利用扩展欧几里得算法可以求解该问题 [代码] #include <algorithm> #include <cstring> #include <cstdio> using name

POJ 3253 Fence Repair (优先队列)

POJ 3253 Fence Repair (优先队列) Farmer John wants to repair a small length of the fence around the pasture. He measures the fence and finds that he needsN (1 ≤ N ≤ 20,000) planks of wood, each having some integer lengthLi (1 ≤ Li ≤ 50,000) units. He the

POJ 3292 Semi-prime H-numbers(数)

Semi-prime H-numbers Description This problem is based on an exercise of David Hilbert, who pedagogically suggested that one study the theory of 4n+1 numbers. Here, we do only a bit of that. An H-number is a positive number which is one more than a m

POJ 3301 Texas Trip (三分)

题目链接 题意 : 给你若干个点,让你找最小的正方形覆盖这所有的点.输出面积. 思路 : 三分枚举正方形两对边的距离,然后求出最大,本题用的是旋转正方形,也可以用旋转点,即点的相对位置不变. 正方形从0度到180度变化的过程中,把所有点覆盖,面积肯定是有一个最小峰值,是一个凸函数.因此可以用三分法解决.这里有一个难点就是已知两个定点的x,y坐标,过这两个点做两条平行线,平行线并与x轴成d度的角,怎么算出两条平行线的距离. d1 = fabs(cos(d)*(yi-yj)-sin(d)*(xi-x

POJ 1699 Best Sequence(DFS)

題目鏈接 題意 : 將幾個片段如圖所示方法縮成一個序列,求出最短這個序列. 思路 : 其實我也不知道怎麼做.....看網上都用了DP.....但是我不會.....這個DP不錯,還有用KMP+状压DP做的 1 //1699 2 #include <iostream> 3 #include <stdio.h> 4 #include <string.h> 5 #include <string> 6 7 using namespace std; 8 9 string

POJ 1160 Post Office (动态规划)

Post Office Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 15412   Accepted: 8351 Description There is a straight highway with villages alongside the highway. The highway is represented as an integer axis, and the position of each villa

poj 3440 Coin Toss(概率)

http://poj.org/problem?id=3440 大致题意:给出一个n*m的格子,每个格子的边长为t,随意抛一枚硬币并保证硬币的圆心在格子里或格子边上,硬币的直径为c,求硬币覆盖格子的个数的概率. 思路:高中的概率题,ms是几何概型.根据分别覆盖1,2,3,4个格子时圆心可分部的面积比上总面积就是答案. #include <stdio.h> #include <iostream> #include <algorithm> #include <set&g

POJ题目Java代码(一)

POJ 1001 Exponentiation import java.math.BigDecimal; import java.util.Scanner; public class Poj1001 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while(sc.hasNext()){ BigDecimal bigDecimal = new BigDecimal(sc.next())

POJ 3177 Redundant Paths(Tarjan)

题目链接 题意 : 一个无向连通图,最少添加几条边使其成为一个边连通分量 . 思路 :先用Tarjan缩点,缩点之后的图一定是一棵树,边连通度为1.然后找到所有叶子节点,即度数为1的节点的个数leaf,最后要添加的边的条数就是(leaf+1)/2 : 1 // 3177 2 #include <cstdio> 3 #include <cstring> 4 #include <iostream> 5 #include <algorithm> 6 7 using