nefu 630 Min Chain(扩展欧几里得)
2014-05-02 18:31
441 查看
Min Chain | ||
| ||
description | ||
Raven likes games of numbers. Today he meets two numbers and thinks whether he could get a result of 1 by doing at least one operation (addition or subtraction). However, he is tired of calculation; he also wants to know the minimum steps of operation that he could get 1. | ||
input | ||
The first line of the input contains an integer T, which indicates the number of test cases. In the following T rows, there are two positive integers a, b ( 0<=a, b<=10^9) in each row. | ||
output | ||
For each case, output the least number of steps. If you cannot get 1, just output -1. | ||
sample_input | ||
Sample Input 3 3 2 16 9 6 8 | ||
sample_output | ||
1 10 -1 | ||
hint | ||
Sample 1: 3 - 2 = 1, One subtraction will be needed. Sample 2: 16-9+16-9+16-9-9-9+16-9-9=1,It requires 10 additions and subtractions. Sample 3: You cannot get 1. |
经典扩展欧几里得算法
#include<iostream> #include<cstring> #include<cmath> #include<cstdio> #include<stack> #include<algorithm> using namespace std; long long a,b; long long exgcd(long long m,long long &x,long long n,long long &y) { long long x1,y1,x0,y0; x0=1;y0=0; x1=0;y1=1; long long r=(m%n+n)%n; long long q=(m-r)/n; x=0;y=1; while(r) { x=x0-q*x1;y=y0-q*y1;x0=x1;y0=y1; x1=x; y1=y; m=n; n=r;r=m%n; q=(m-r)/n; } return n; } int main() { int t; long long x,y; cin>>t; while(t--) { scanf("%d%d",&a,&b); if(b>a) swap(a,b); if(b==0) { if(a==1) printf("1\n"); else printf("-1\n"); } else if(b==1&&a==2) printf("1\n"); else if(b==1) printf("2\n"); else { long long d=exgcd(a,x,b,y); //cout<<x<<"&&"<<y<<endl; if(d!=1) printf("-1\n"); else { long long c=1,e; e=x*c/b; x=c*x-e*b; y=(1-a*x)/b; long long ans=fabs(x)+fabs(y); printf("%lld\n",ans-1); } } } return 0; }
相关文章推荐
- nefu84 五指山(扩展欧几里得)
- NEFU 84 扩展欧几里得
- [数论]HOJ 2185 Min Chain 扩展欧几里得算法
- nefu 630 Min Chain
- NEFU 630 数论扩展欧几里得算法的应用
- NEFU 84 五指山(扩展欧几里得)
- NEFU 84 五指山 (扩展欧几里得)
- nefu 84 五指山 (扩展欧几里得)
- nyoj-775-整数性质(扩展欧几里得定理)
- 扩展欧几里得extgcd
- 扩展欧几里得的一些思考
- POJ-----2891---Strange Way to Express Integers扩展欧几里得
- Java实现算法导论中求解模线性方程解(基于最大公约数欧几里得扩展算法)
- 扩展欧几里得
- POJ 1061 青蛙的约会 (扩展欧几里得定理)
- 欧几里得 与 扩展欧几里得
- 扩展欧几里得模板(洛谷1082 同余方程NOIP 2012 提高组 第二天 第一题)
- 扩展欧几里得
- 欧几里得原理及扩展欧几里得原理(Euclidean Theory and Extended Euclidean Theory)学习笔记
- 扩展欧几里得