https://www.hackerrank.com/contests/infinitum16-firsttimer/challenges/solve-equations

给定一条方程a*x + b*y = c

保证有解的情况下,我们要求一个点,满足x > 0且 这个点到原点的欧几里德距离最短

根据扩展欧几里德算法,我们能求得一组(x1,y1)满足x1 > 0的解,第一个x>0的解。

然后通解就是x2 = x1 + b/abgcd*k  y2 = y1 - a/abgcd*k

如果(x2,y2)就是所求,那么k一定要大于0,因为小于0的话,加上x1的时候会使得x2<0,这是因为x1是所有解中最小的正整数了,在它左边的解,必定是小于0的。

那么k的范围就是 >= 0了

把式子展开  x2*x2 + y2*y2

得到一条关于t的二次方程,如果对称轴在原点左边,那么t=0就是ans

否则,取对称轴。因为可能是小数,所以取整了,判断对称轴左右两个点就好了

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
LL abGCD;
LL gcd (LL a, LL b) {
if (a % b == ) return b;
else return gcd (b, a % b);
}
LL exgcd (LL a,LL mod,LL &x,LL &y) {
if (mod==) {
x=;
y=;
return a;
}
LL g=exgcd(mod,a%mod,x,y);
LL t=x;
x=y;
y=t-(a/mod)*y;
return g;
}
LL get_min_number (LL a,LL b,LL c,LL &x,LL &y) {
abGCD = gcd(a,b);
if (c%abGCD != ) return -;
a /= abGCD;
b /= abGCD;
c /= abGCD;
LL tx,ty;
exgcd(a,b,tx,ty);
x = tx*c;
y = c*ty;
LL temp_x = x;
x %= b;
if (x<=) x += b;
LL k = (temp_x-x)/b;
y += k*a;
return ;
} void work () {
LL a, b, c;
cin >> a >> b >> c;
LL x, y;
get_min_number (a, b, c, x, y);
// LL bb=abs(b/gcd(a,b));
// LL aa=abs(a/gcd(a,b));
// while(x<0) x += bb;
// while(x*a+b*y!=c) y += aa;
// cout << x << " " << y << endl;
LL c1 = b / abGCD;
LL c2 = - a / abGCD;
LL A = c1 * c1 + c2 * c2;
LL B = * y * c2 + * x * c1;
LL C = x * x + y * y;
if (B > ) {
cout << x << " " << y << endl;
} else {
LL t = B / (- * A);
LL mx = A*t*t + B*t + C;
t++;
LL tmax = A*t*t + B*t + C;
if (tmax > mx) {
t--;
}
cout << x + c1*t << " " << y + c2*t << endl;
}
} int main () {
#ifdef local
freopen("data.txt","r",stdin);
#endif
int t;
cin >> t;
while (t--) work ();
return ;
}

1
4 1 82

Solve Equations HackerRank 扩展欧几里德 && 数学的更多相关文章

  1. CodeForces 146E - Lucky Subsequence DP+扩展欧几里德求逆元

    题意: 一个数只含有4,7就是lucky数...现在有一串长度为n的数...问这列数有多少个长度为k子串..这些子串不含两个相同的lucky数... 子串的定义..是从这列数中选出的数..只要序号不同 ...

  2. HDU 2669 Romantic 扩展欧几里德---->解不定方程

    Romantic Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  3. 扩展欧几里德 SGU 106

    题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=106   题意:求ax + by + c = 0在[x1, x2], [y1, y2 ...

  4. HDU 2669 Romantic(扩展欧几里德)

    题目链接:pid=2669">http://acm.hdu.edu.cn/showproblem.php?pid=2669 Problem Description The Sky is ...

  5. 扩展欧几里德 poj1061 青蛙的约会

    扩展欧几里德很经典.可是也有时候挺难用的.一些东西一下子想不明确.. 于是来了一个逆天模板..仅仅要能列出Ax+By=C.就能解出x>=bound的一组解了~ LL exgcd(LL a, LL ...

  6. (扩展欧几里德算法)zzuoj 10402: C.机器人

    10402: C.机器人 Description Dr. Kong 设计的机器人卡尔非常活泼,既能原地蹦,又能跳远.由于受软硬件设计所限,机器人卡尔只能定点跳远.若机器人站在(X,Y)位置,它可以原地 ...

  7. [BZOJ1407][NOI2002]Savage(扩展欧几里德)

    题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1407 分析: m,n范围都不大,所以可以考虑枚举 先枚举m,然后判定某个m行不行 某个 ...

  8. 欧几里德与扩展欧几里德算法 Extended Euclidean algorithm

    欧几里德算法 欧几里德算法又称辗转相除法,用于计算两个整数a,b的最大公约数. 基本算法:设a=qb+r,其中a,b,q,r都是整数,则gcd(a,b)=gcd(b,r),即gcd(a,b)=gcd( ...

  9. 51nod 1352 扩展欧几里德

    给出N个固定集合{1,N},{2,N-1},{3,N-2},...,{N-1,2},{N,1}.求出有多少个集合满足:第一个元素是A的倍数且第二个元素是B的倍数. 提示: 对于第二组测试数据,集合分别 ...

随机推荐

  1. socket发送结构体

    struct send_info {char info_from[20]; //发送者IDchar info_to[20]; //接收者IDint info_length; //发送的消息主体的长度c ...

  2. cocos2d-x 屏幕分辨率适配方法

    转自:http://blog.csdn.net/somestill/article/details/9950403 bool AppDelegate::applicationDidFinishLaun ...

  3. 开启struts2自带的开发模式常量

    在以前的开发中,当修改一些配置时总是不能及时地更新到服务器,我们总会重新部署或重启来更新改变的内容,在struts2中可以通过一个常量来达到此目的.即在struts.xml中的<struts&g ...

  4. C/C++中变量类型最值之宏定义

    C/C++ [climits(limits.h)] CHAR_BIT        Number of bits for a char object (byte)                    ...

  5. elasticsearch 复合查询

    常用查询 固定分数查询 127.0.0.1/_search(全文搜索) { "query":{ "match"{ "title":" ...

  6. assert.fail()

    assert.fail(message) assert.fail(actual, expected[, message[, operator[, stackStartFunction]]]) oper ...

  7. cocos2dx v3.x lua绑定分析

    打算新项目转到cocos2dx v3上了,下载代码浏览过后发现改动真是非常大,结构性调整很多. 比如tolua绑定这一块,就几乎全翻新了. 胶水代码的生成,改成了全自动式的,通过clang来分析c++ ...

  8. 32、Differential Gene Expression using RNA-Seq (Workflow)

    转载: https://github.com/twbattaglia/RNAseq-workflow Introduction RNAseq is becoming the one of the mo ...

  9. 《精通Spring4.X企业应用开发实战》读后感第六章(属性编辑器)

  10. 7.30实习培训日志-SQL优化

    总结 今天早上考试,下午主要是老师引导我们学习SQL优化,晚上主要是同学的技术分享,杨松柏同学主要给我们分享了java的io的一些东西,c10k问题,bio(同步阻塞IO),NIO(同步非阻塞IO), ...