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

嗯... 题目链接:http://poj.org/problem?id=2142 AC代码: #include<cstdio> #include<iostream> using namespace std; inline int _abs(int x){ ) return -x; return x; } inline void exgcd(int a, int b, int &g, int &x, int &y){ ; y = ;} else { exgcd…
d.用2种砝码,质量分别为a和b,称出质量为d的物品.求所用的砝码总数量最小(x+y最小),并且总质量最小(ax+by最小). s.扩展欧几里得求解不定方程. 设ax+by=d. 题意说不定方程一定有解.对于不定整数方程pa+qb=c,若 c mod Gcd(p, q)=0,则该方程存在整数解,否则不存在整数解. 也就是说,d mod gcd(a,b)=0. a,b,d同时除以gcd(a,b)得到a'x+b'y=d'; 用扩展欧几里德求出 a'x+b'y=gcd(a',b')=1的解(x,y),…
这题实际解不定方程:ax+by=c只不过题目要求我们解出的x和y 满足|x|+|y|最小,当|x|+|y|相同时,满足|ax|+|by|最小.首先用扩展欧几里德,很容易得出x和y的解.一开始不妨令a>b,若a<=b,则交换a和b.设d=gcd(a,b),最终的则x=x0+b/d*t,y=y0-a/d*tz=|x|+|y|=|x0+b/d*t|+|y0-a/d*t|实际上就是求z=|a1*t+c1|+|c2-a2*t|在t取何值时最小.(a2>a1)首先由不定方程ax+by=c,a>…
POJ 2115:http://poj.org/problem?id=2115 思路 设循环T次 则要满足A≡(B+CT)(mod 2k) 可得 A=B+CT+m*2k 移项得C*T+2k*m=B-A (因为要满足B大于A)即是Exgcd的标准式子了 代码 #include<iostream> #include<cstdio> using namespace std; #define ll long long ll A,B,C,T,k; int gcd(ll a,ll b) { i…
[题目链接] 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.2142 The Balance (拓展欧几里得) 题意分析 现有2种质量为a克与b克的砝码,求最少 分别用多少个(同时总质量也最小)砝码,使得能称出c克的物品. 设两种砝码分别有x个与y个,那么有ax+by=c.可用拓展欧几里得求解. 若x与y均为正数,说明在天平的同一侧,否则在不同册. 需要注意的是,求出的x与y仅为一组特解,此时需要求|x| + |y| 的最小值.根据: 可得 显然这是不好求的,但我们不妨设a>b,根据斜率关系,不难作图. 可以看出,最小值在t2附近取得,枚举附近值…
POJ 3669 Meteor Shower(流星雨) Time Limit: 1000MS    Memory Limit: 65536K Description 题目描述 Bessie hears that an extraordinary meteor shower is coming; reports say that these meteors will crash into earth and destroy anything they hit. Anxious for her sa…
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 3071] Football(DP) Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4350   Accepted: 2222 Description Consider a single-elimination football tournament involving 2n teams, denoted 1, 2, -, 2n. In each round of the tournament, all tea…
Poj 3613 Cow Relays (图论) 题目大意 给出一个无向图,T条边,给出N,S,E,求S到E经过N条边的最短路径长度 理论上讲就是给了有n条边限制的最短路 solution 最一开始想到是的去直接统计最短路经过了多少条边,结果,,, 还是太年轻了... 不过,看数据范围只有1000,那么floyd是首选 回顾Floyd算法流程,其中的i到j松弛操作是通过k完成的 那么松弛一次就利用一个k点,我现在要经过n条边,那么松弛n次即可 详细说就是更新一次之后,把f[i][j]拷贝到原来的…
0.前言 相信大家对于欧几里得算法都已经很熟悉了.再学习数论的过程中,我们会用到扩展欧几里得算法(exgcd),大家一定也了解过.这是本蒟蒻在学习扩展欧几里得算法过程中的思考与探索过程. 1.Bézout定理 扩展欧几里得算法利用归纳法,证明了Bézout定理. Bézout定理:对于任意整数 \(a\),\(b\) ,存在一对整数 \(x\),\(y\),满足 \(ax+by=gcd(a,b)\) 在扩展欧几里得的算法中,我们求出 \(x\),\(y\) 的值. 2.证明 2.1 \(gcd\…
D - Jungle Roads Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 1251 Description The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid money was spent on extr…
转载请注明博客地址:http://blog.csdn.net/suileisl HDFS集群balance,对应版本balance design 6 如需word版本,请QQ522173163联系索要,谢谢. 对接(1)中的图文 作者简介  昵称:根根 姓名:隋磊 QQ:522173163 email:rsuilei@163.com  本文的翻译,离不开<至高天>系统架构师团队的支持  猫头哥:http://phl.iteye.com/  根根:http://blog.csdn.net/sui…
转载请注明博客地址:http://blog.csdn.net/suileisl HDFS集群balance,对应版本balance design 6 如需word版本,请QQ522173163联系索要,谢谢. 对接(2)中的图文 作者简介  昵称:根根 姓名:隋磊 QQ:522173163 email:rsuilei@163.com  本文的翻译,离不开<至高天>系统架构师团队的支持  猫头哥:http://phl.iteye.com/  根根:http://blog.csdn.net/sui…
转载请注明博客地址:http://blog.csdn.net/suileisl HDFS集群balance,对应版本balance design 6 如需word版本,请QQ522173163联系索要,谢谢. 对接(3)中的图文. 作者简介  昵称:根根 姓名:隋磊 QQ:522173163 email:rsuilei@163.com  本文的翻译,离不开<至高天>系统架构师团队的支持  猫头哥:http://phl.iteye.com/  根根:http://blog.csdn.net/su…
Primitive Roots http://poj.org/problem?id=1284 Time Limit: 1000MS   Memory Limit: 10000K       Description We say that integer x, 0 < x < p, is a primitive root modulo odd prime p if and only if the set { (xi mod p) | 1 <= i <= p-1 } is equal…
1.欧几里得算法(辗转相除法) 直接上gcd和lcm代码. int gcd(int x,int y){ ?x:gcd(y,x%y); } int lcm(int x,int y){ return x*y/gcd(x,y); } 2.扩欧:exgcd:对于a,b,一定存在整数对(x,y)使ax+by=gcd(a,b)=d ,且a,b互质时,d=1. x,y可递归地求得. 我懒得改返回值类型了 long long exgcd(long long a,long long b,long long &x,…
题目链接 题意 : N头牛,每个牛的坏脾气都有一个值,每个值都不相同,把这个值按照从小到大排序,如果两个值交换,那么会花掉这两个值之和的时间,让你花最少的时间将每个值从小到大排好序,求最小的总时间. 思路 : 这个在黑书上有写,就是置换群,248页有写.写的挺详细的.每个状态都可以分为若干个循环的乘积.对于任意循环 i ,设其长度为ki,则至少需要交换ki-1次,即每次让一个元素到达目标位置,而当第ki-1个元素到达目标以后显然第ki个也已经到达目标.第一个方法是让循环中最小的元素t参加所有的交…
Description Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the other. Each small island is a point locating in the sea side. And any radar installation, locating on the coasting, can only cover d distance, s…
Curling 2.0 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12158   Accepted: 5125 Description On Planet MM-21, after their Olympic games this year, curling is getting popular. But the rules are somewhat different from ours. The game is…
Description We have received an order from Pizoor Communications Inc. for a special communication system. The system consists of several devices. For each device, we are free to choose from several manufacturers. Same devices from two manufacturers d…
Description Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def" then a*b = "abcdef". If we think of concatenation as multiplication, exponentiation by a non-negative inte…
https://vjudge.net/problem/POJ-1019 题意 给一串1 12 123 1234 12345 123456 1234567 12345678 123456789 12345678910 1234567891011这种形式的串,问这个串的第i个位置的数字是什么. 分析 这道题的重点在于到第k组时应该为几位数,即对于某个数x,它应该为几位数.答案是log10(x)+1.这样剩下的便是打表预处理了,找出每组的起始位置,再处理出一个最长的组. #include<iostre…
Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 25165   Accepted: 7751 Description The Department of National Defence (DND) wishes to connect several northern outposts by a wireless network. Two different communication technologies are t…
Linear world Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 4426   Accepted: 1006 Description The Disc, being flat, has no real horizon. Any adventurous sailors who get funny ideas from staring at eggs and oranges for too long and set o…
题目链接 Description Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The stalls are located along a straight line at positions x1,...,xN (0 <= xi <= 1,000,000,000). His C (2 <= C <= N) cows don't like this barn layo…
Description Dearboy, a goods victualer, now comes to a big problem, and he needs your help. In his sale area there are N shopkeepers (marked from 1 to N) which stocks goods from him.Dearboy has M supply places (marked from 1 to M), each provides K di…
Description Let N be the set of all natural numbers {0 , 1 , 2 , . . . }, and R be the set of all real numbers. wi, hi for i = 1 . . . n are some elements in N, and w0 = 0.  Define set B = {< x, y > | x, y ∈ R and there exists an index i > 0 such…
Description Andy is fond of old computers. He loves everything about them and he uses emulators of old operating systems on his modern computer. Andy also likes writing programs for them. Recently he has decided to write a text editor for his favorit…
Description A plotter is a vector graphics printing device that connects to a computer to print graphical plots. There are two types of plotters: pen plotters and electrostatic plotters. Pen plotters print by moving a pen across the surface of a piec…