[COCI2020/2021]Euklid
壹、题目描述 ¶
定义函数
给定
贰、题解 ¶
显然一道构造题,但是挺难想到。下面我们不妨假设
首先想到的就是去考察
但是我们可以利用人类智慧,首先一定有
也即,有
但是我们可以考虑,当
根据辗转相除法,我们有
如果设
然后,你会发现你错了,举个栗子:当
也就是说,如果
至于如何找到这个
因为这个
只需要找到这个
但是值得注意的是,直接取
总复杂度
题解过于简单,直接给出构造方法:
b=g\left\lceil{h^K\over g}\right\rceil,a=hb+g\quad(g<h^K) 我只能说,这个构造方法没有时间还真想不到,或许只有
\sf SYDevil 和\sf OID 可以直接出吧?
叁、参考代码 ¶
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<vector>
using namespace std;
// #define NDEBUG
#include<cassert>
namespace Elaina{
#define rep(i, l, r) for(int i=(l), i##_end_=(r); i<=i##_end_; ++i)
#define drep(i, l, r) for(int i=(l), i##_end_=(r); i>=i##_end_; --i)
#define fi first
#define se second
#define mp(a, b) make_pair(a, b)
#define Endl putchar('\n')
#define mmset(a, b) memset(a, b, sizeof a)
// #define int long long
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
template<class T>inline T fab(T x){ return x<0? -x: x; }
template<class T>inline void getmin(T& x, const T rhs){ x=min(x, rhs); }
template<class T>inline void getmax(T& x, const T rhs){ x=max(x, rhs); }
template<class T>inline T readin(T x){
x=0; int f=0; char c;
while((c=getchar())<'0' || '9'<c) if(c=='-') f=1;
for(x=(c^48); '0'<=(c=getchar()) && c<='9'; x=(x<<1)+(x<<3)+(c^48));
return f? -x: x;
}
template<class T>inline void writc(T x, char s='\n'){
static int fwri_sta[1005], fwri_ed=0;
if(x<0) putchar('-'), x=-x;
do fwri_sta[++fwri_ed]=x%10, x/=10; while(x);
while(putchar(fwri_sta[fwri_ed--]^48), fwri_ed);
putchar(s);
}
}
using namespace Elaina;
ull R(ull a, ull b){
if(a<b) return R(b, a);
if(b==1) return a;
return R(a/b, b);
}
ull gcd(ull a, ull b){ return b? gcd(b, a%b): a; }
ull g, h, k, tmp, i, a, b;
signed main(){
freopen("euklid.in", "r", stdin);
freopen("euklid.out", "w", stdout);
rep(_, 1, readin(1)){
g=readin(1llu), h=readin(1llu);
tmp=1, k=0;
while(g>=tmp) ++k, tmp*=h;
// When x,y are integers the equation ceil(x/y)=(x+y-1)/y is established
i=(tmp+g-1)/g, b=g*i, a=h*b+g;
writc(a, ' '), writc(b);
}
return 0;
}