P11063 【MX-X4-T3】「Jason-1」數對變換

dcytrl發表於2024-09-20

題意

你有一個有序數對 \((x,y)\),每次你可以選擇其中一個數並指定一個整數 \(k\),然後將你選的那個數除以 \(k\) 下取整,另外一個數乘 \(k\)。你現在想要把 \((a,b)\) 變換成 \((c,d)\) 構造一組在 65 步解決問題的方案,或報告無解。

\(1\le a,b,c,d\le 10^9\)

分析

這題的突破口在於特殊性質 A。不往這方面想基本上就寄飛了。

考慮特殊性質 A:\(ab=cd\)。我們有如下構造方案:\((a,b)\rightarrow (1,ab=cd)\rightarrow (c,d)\)

考慮更一般的情況。

首先不難發現 \(ab\) 的乘積單調不升,那麼 \(ab<cd\) 一定無解。

其次,我們考慮讓 \(ab\) 的大小盡可能的接近 \(cd\)。假設我們對第一個數操作,考慮一次操作 \((x,y)\rightarrow(\lfloor\dfrac{x}{k}\rfloor,yk)\)\(xy\) 的乘積減小了 \(xy-(\lfloor\dfrac{x}{k}\rfloor\cdot k)\cdot y=xy-(x-x\bmod k)\cdot y=(x\bmod k)\cdot y\)。根據經典結論,\(a\bmod b< \dfrac{a}{2}\),我們想要儘可能的讓 \(ab\) 減小的更快,那麼我們就想讓 \(x\bmod k\) 的值儘可能大,取 \(k=\dfrac{x}{2}+1\) 即可。這樣我們每次對 \((x,y)\) 操作 \(k\),直到 \(cd\le xy<2cd\)

注意我們無法對 \(1\) 進行任何操作,此時我們需要將這兩個數交換後繼續做。
而當另一個數為 \(2\) 時,我們只能把這個數除 \(2\),並把另外的數乘 \(2\),這樣的話 \((1,2)\rightarrow (2,1)\rightarrow \cdots\),就死迴圈了。只有當 \(1\cdot 2=2\ge 2cd\) 時會出現這種情況,此時 \(cd=1\),故當 \(cd=1\) 時只有 \(ab=1\) 時才有解,其他情況無解。

\(cd\le ab<2cd\) 時,類位元殊性質 A 的思想,我們有如下構造方案:\((a,b)\rightarrow (ab,1)\rightarrow_{k=cd} (\lfloor\dfrac{ab}{cd}\rfloor=1,cd)\rightarrow (c,d)\)

至此這道題做完了,使用次數 \(\log ab+3\),在 65 的次數限制下足以透過。

點選檢視程式碼
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<map>
#include<vector>
#include<queue>
#include<stack>
#include<bitset>
#include<set>
#include<ctime>
#include<random>
#include<cassert>
#define IOS ios::sync_with_stdio(false)
#define PY puts("Yes")
#define PN puts("No")
#define PW puts("-1")
#define P0 puts("0")
#define P__ puts("")
#define PU puts("--------------------")
#define mp make_pair
#define fi first
#define se second
#define pc putchar
#define pb emplace_back
#define un using namespace
#define popc __builtin_popcountll
#define all(x) x.begin(),x.end()
#define rep(a,b,c) for(int a=(b);a<=(c);++a)
#define per(a,b,c) for(int a=(b);a>=(c);--a)
#define reprange(a,b,c,d) for(int a=(b);a<=(c);a+=(d))
#define perrange(a,b,c,d) for(int a=(b);a>=(c);a-=(d))
#define graph(i,j,k,l) for(int i=k[j];i;i=l[i].nxt)
#define lowbit(x) (x&-x)
#define lson(x) (x<<1)
#define rson(x) (x<<1|1)
#define mem(x,y) memset(x,y,sizeof x)
//#define double long double
#define int long long
//#define int __int128
using namespace std;
using pii=pair<int,int>;
using i64=long long;
using u64=unsigned long long;
template<typename T>bool greating(T x,T y){return x>y;}
inline int rd(){
	int x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+ch-48;ch=getchar();}return x*f;
}
template<typename T>
inline void write(T x,char ch='\0'){
	if(x<0){x=-x;putchar('-');}
	int y=0;char z[40];
	while(x||!y){z[y++]=x%10+48;x/=10;}
	while(y--)putchar(z[y]);if(ch!='\0')putchar(ch);
}
bool Mbg;
const int maxn=2e5+5,maxm=4e5+5,inf=0x3f3f3f3f;
const long long llinf=0x3f3f3f3f3f3f3f3f;
int a,b,c,d;
vector<pii>v;
void rev(int &x){x=x==1?2:1;}
inline void solve_the_problem(){
	a=rd(),b=rd(),c=rd(),d=rd(),v.clear();
	if(a*b<c*d||(a*b!=1&&c*d==1))return (void)PW;
	int t=1;
	while(a*b>=2*c*d){
		if(a==1)rev(t),swap(a,b);
		int r=a/2+1;
		a/=r,b*=r,v.emplace_back(mp(t,r));
	}
	if(t==2)swap(a,b);
	v.emplace_back(mp(2,b));
	v.emplace_back(mp(1,c*d));
	v.emplace_back(mp(2,c));
	write(v.size(),10);
	for(pii i:v)write(i.fi,32),write(i.se,10);
}
bool Med;
signed main(){
//	freopen(".in","r",stdin);freopen(".out","w",stdout);
//	fprintf(stderr,"%.3lfMB\n",(&Mbg-&Med)/1048576.0);
	int _=rd();
	while(_--)solve_the_problem();
}
/*

*/

相關文章