Romatic HDU2699
来源:互联网 发布:金蝶软件补丁 编辑:程序博客网 时间:2024/05/17 00:15
题意:给两个非负整数 a 和 b,要找到一个非负整数 x 和一个整数 y,使得满足
思路:用扩展欧几里得,因为要求 x 非负,所以可以把式子写成
代码:
#include<cstdio>#include<cstring>#include<cstdlib>#include<stack>#include<queue>#include<utility>#include<vector>#include<cmath>#include<set>#include<map>#include<iostream>#include<algorithm>using namespace std;typedef long long LL;LL ExtendGcd(LL a, LL b, LL& x, LL& y){ LL r; if(b == 0){ x = 1; y = 0; return a; } else{ r = ExtendGcd(b, a%b, y, x); y -= (a/b)*x; } return r;}int main(){ LL a, b; while(scanf("%lld%lld", &a, &b) == 2){ LL x, y; if(ExtendGcd(a, b, x, y) == 1){ while(x < 0){ x += b; y -= a; } printf("%lld %lld\n", x, y); } else{ printf("sorry\n"); } } return 0;}
阅读全文
0 0
- Romatic HDU2699
- hdu2699
- HDU2699+Easy
- HDU2699 扩展欧几里德
- Romantic(hdu2699+欧几里德)
- hdu2699 Five in a Row 暴力穷举
- 判断网络状态
- 6 执行计划--优化主题系列
- bzoj 1032: [JSOI2007]祖码Zuma(区间DP)
- 剑指offer之十二---调整数组顺序使奇数位于偶数前面
- 003-js缓动动画的一个小bug
- Romatic HDU2699
- linux的tar zcvf,xvf的区别
- C++ 变量周期
- 自定义TextView
- 讯飞语音Demo—java版
- Quiz项目java代码部分
- WMwar虚拟机以及linux操作系统的安装
- MXNET学习笔记(一):Module类(1)
- Java Scaner类详解_动力节点Java学院整理