poj 1061 青蛙的约会(扩展欧几里得)
来源:互联网 发布:网络歌曲伤不起歌词 编辑:程序博客网 时间:2024/05/18 09:06
http://poj.org/problem?id=1061
思路:设它们跳了t次相遇,那么有 (x+t*m)-(y+t*n) = z*l(z是一个整数,表示它们路程差是l的z倍),变形得
(n-m)*t + z*l = (x-y);
令 a = n-m; b = l; c = x-y;
那么原式变为 a*t + z*b = c;
扩展欧几里得模板,求解形如a*x + b*y = gcd(a,b)方程。
LL extend_gcd(LL a, LL b, LL &x, LL &y){if(b == 0){x = 1;y = 0;return a;}LL d = extend_gcd(b,a%b,x,y);LL t = x;x = y;y = t-a/b*y;return d;}
方程a*x + b*y = c有解的前提是 gcd(a,b) | c,在这个基础上方程有d=gcd(a,b)个不同的解。其中基础解x0 = x'*(c/d)%b(其中x'为a*x'+b*y' = gcd(a,b)的解);通解为xi = x0 + i * (b/d)。
#include <stdio.h>#include <iostream>#include <algorithm>#include <set>#include <map>#include <vector>#include <math.h>#include <string.h>#include <queue>#include <string>#include <stdlib.h>#define LL long long#define _LL __int64#define eps 1e-8using namespace std;const int INF = 0x3f3f3f3f;const int maxn = 10;LL extend_gcd(LL a, LL b, LL &x, LL &y){if(b == 0){x = 1;y = 0;return a;}LL d = extend_gcd(b,a%b,x,y);LL t = x;x = y;y = t-a/b*y;return d;}int main(){LL x,y,m,n,l;LL a,b,c,d;while(~scanf("%lld %lld %lld %lld %lld",&x,&y,&m,&n,&l)){a = n-m;b = l;c = x-y;LL t,z;d = extend_gcd(a,b,t,z);if(c%d != 0){printf("Impossible\n");continue;}t = t*(c/d);t = (t%b+b)%b;printf("%lld\n",t);}return 0;}
0 0
- POJ 1061 青蛙的约会 扩展欧几里得
- poj 1061 青蛙的约会 扩展欧几里得
- poj 1061青蛙的约会(扩展欧几里得)
- POJ 1061 青蛙的约会( 扩展欧几里得)
- poj 1061 青蛙的约会(扩展欧几里得)
- Poj 1061 青蛙的约会(扩展欧几里得)
- POJ-1061-青蛙的约会(扩展欧几里得)
- poj 1061 青蛙的约会 【扩展欧几里得】
- poj 1061 青蛙的约会(扩展欧几里得)
- POJ 1061 青蛙的约会 扩展欧几里得
- POJ 1061 青蛙的约会 (扩展欧几里得)
- poj 1061 青蛙的约会 【扩展欧几里得】
- POJ 1061 青蛙的约会(扩展欧几里得)
- POJ 1061 青蛙的约会(扩展欧几里得)
- poj 1061 青蛙的约会 【扩展欧几里得】
- Poj 1061 青蛙的约会(扩展欧几里得)
- poj 1061 青蛙的约会 扩展欧几里得
- poj-1061-青蛙的约会【扩展欧几里得】
- 密码学中一些加减乘除法优化
- DOM和DHTML等,复习总结
- MFC中定义全局变量和全局函数的方法
- XTU OJ 1209 Alice and Bob 2014(嘉杰信息杯ACM/ICPC湖南程序设计邀请赛暨第六届湘潭市程序设计竞赛)
- 华为编码达人秀(第二季) Olympic Game
- poj 1061 青蛙的约会(扩展欧几里得)
- Android 自定义View,实现折线图
- <MFC笔记>多线程编程简单实例
- OpenCV: OpenCV中IplImage图像格式
- 收藏的网页-Ogre
- Acdream 1096 签到 异或思维题
- [Unity3D]Unity3D游戏开发之从《魂斗罗》游戏说起(上)——目标追踪
- OpenCV Machine Learning 之 支持向量机 (Support Vector Machine)
- 数据库的基本应用