题解:P11063 【MX-X4-T3】「Jason-1」数对变换
InformationEntropy · · 题解
题目分析
每次变换,
故只需考虑
又有
当且仅当
这意味着,经过若干次操作后,数对中两数的积不会增加,若初始
考虑
(操作 2 同理。)
由于
对于
最终变化:
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long ll;
template<class T>inline void read(T &x){
x=0;
int f=0;
char c=getchar();
for(;!isdigit(c);c=getchar()) f^=!(c^45);
for(;isdigit(c);c=getchar()) x=(x<<1)+(x<<3)+(c^48);
if(f) x=-x;
}
ll gcd(ll a,ll b){
if(a%b == 0){
return b;
}
return gcd(b,a%b);
}
struct node{
ll op,k;
}ans[65];
int main(){
int T;
read(T);
ll a,b,c,d;
for(int i=1; i<=T; i++){
read(a);
read(b);
read(c);
read(d);
ll k = a*b-c*d;
if(c*d == 1 && a*b != 1){
cout << -1 << endl;
continue;
}
if(a==c && b==d){
cout << 0 << endl;
continue;
}//这两个特判不要忘
if(k == 0){
cout << 2 << endl;
cout << 1 << " " << a/gcd(a,c) << endl;
cout << 2 << " " << c/gcd(a,c) << endl;
}else if(k < 0){
cout << -1 << endl;
}else{
ll u=a*b,v=c*d;
int cnt = 0;
ans[++cnt].op = 2;
ans[cnt].k = b;
int op = 0;
while(u >= (v<<1)){
ans[++cnt].op = op+1;
ans[cnt].k = (u>>1)+1;
u = (u>>1)+1;
op ^= 1;
}//a,b辗转变为1
if(op == 1){
ans[++cnt].op = 2;
ans[cnt].k = u;
}
ans[++cnt].op = 1;
ans[cnt].k = v;
ans[++cnt].op = 2;
ans[cnt].k = c;
cout << cnt << endl;
for(int j=1; j<=cnt; j++){
cout << ans[j].op << " " << ans[j].k << endl;
}
}
}
return 0;
}