QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#624794#6440. Xingqiu's Jokewwww22#TL 683ms24444kbC++203.1kb2024-10-09 16:38:392024-10-09 16:38:39

Judging History

你现在查看的是最新测评结果

  • [2024-10-09 16:38:39]
  • 评测
  • 测评结果:TL
  • 用时:683ms
  • 内存:24444kb
  • [2024-10-09 16:38:39]
  • 提交

answer

#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define fo(i,a,b) for(ll (i)=(a);(i)<=(b);(i)++)
#define fd(i,a,b) for(ll (i)=(a);(i)>=(b);(i)--)

using namespace std;
const int N=2e5+10;
int n,a,b;
map<pair<int,int>,int> mp;
int c[N];
int cnt=0;


//ll gcd(ll a, ll b) {
//  if (b == 0) return a;
//  return gcd(b, a % b);
//}
//
//ll bmul(ll a, ll b, ll m) {  // 快速乘
//  ull c = (ull)a * (ull)b - (ull)((long double)a / m * b + 0.5L) * (ull)m;
//  if (c < (ull)m) return c;
//  return c + m;
//}
//
//ll qpow(ll x, ll p, ll mod) {  // 快速幂
//  ll ans = 1;
//  while (p) {
//    if (p & 1) ans = bmul(ans, x, mod);
//    x = bmul(x, x, mod);
//    p >>= 1;
//  }
//  return ans;
//}
//
//bool Miller_Rabin(ll p) {  // 判断素数
//  if (p < 2) return 0;
//  if (p == 2) return 1;
//  if (p == 3) return 1;
//  ll d = p - 1, r = 0;
//  while (!(d & 1)) ++r, d >>= 1;  // 将d处理为奇数
//  for (ll k = 0; k < 10; ++k) {
//    ll a = rand() % (p - 2) + 2;
//    ll x = qpow(a, d, p);
//    if (x == 1 || x == p - 1) continue;
//    for (int i = 0; i < r - 1; ++i) {
//      x = bmul(x, x, p);
//      if (x == p - 1) break;
//    }
//    if (x != p - 1) return 0;
//  }
//  return 1;
//}
//
//ll Pollard_Rho(ll x) {
//  ll s = 0, t = 0;
//  ll c = (ll)rand() % (x - 1) + 1;
//  int step = 0, goal = 1;
//  ll val = 1;
//  for (goal = 1;; goal *= 2, s = t, val = 1) {  // 倍增优化
//    for (step = 1; step <= goal; ++step) {
//      t = (bmul(t, t, x) + c) % x;
//      val = bmul(val, abs(t - s), x);
//      if ((step % 127) == 0) {
//        ll d = gcd(val, x);
//        if (d > 1) return d;
//      }
//    }
//    ll d = gcd(val, x);
//    if (d > 1) return d;
//  }
//}
//
//void fac(ll x) {
//  if (x <= max_factor || x < 2) return;
//  if (Miller_Rabin(x)) {              // 如果x为质数
//    max_factor = max(max_factor, x);  // 更新答案
//    return;
//  }
//  ll p = x;
//  while (p >= x) p = Pollard_Rho(x);  // 使用该算法
//  while ((x % p) == 0) x /= p;
//  fac(x), fac(p);  // 继续向下分解x和p
//}



int find(int a,int b)
{
	int fa;
	if(a<=0)return 1e9+10;
	if(a==1)return 0;
	if(b==1)return a-1;
	if(mp.find(make_pair(a,b))!=mp.end()) return mp[make_pair(a,b)];
	int ans=a-1;
	for(int i=0;i<cnt;i++)
	{
		if(b%c[i])
		{
			continue;
		}
		fa=c[i];
		ans=min(ans,1+a%fa+find(a/fa,b/fa));
		ans=min(ans,1+(fa-a%fa)+find(a/fa+1,b/fa));
	}	
	mp[make_pair(a,b)]=ans;
//	cout<<a<<" "<<b<<" "<<ans<<"\n";
	return ans;
}


void solve()
{
	cin>>a>>b;
	
	if(a>b)swap(a,b);
	b=b-a;
	int x=b;
	cnt=0;
	for(int i=2;i*i<=x;i++)
	{
		while(x%i)
		{
			i++;
			if(i*i>x)
			{
				i=x;
			}
		}
		c[cnt]=i;
		cnt++;
//		cout<<x<<"\n";
		while(x%i==0)x/=i;
	}
	if(x>1)
	{
		c[cnt]=x;
		cnt++;
	}
//	for(int i=0;i<cnt;i++)
//	{
//		cout<<c[i]<<" ";
//	}
	cout<<find(a,b)<<"\n";
}
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
//	init();
//	for(int i=0;i<cnt;i++)
//	{
//		cout<<pr[i]*pr[i]<<" ";
//	}
//	cout<<"\n";
	int T=1;
	cin>>T;
	while(T--) solve();
	return 0;
} 

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3676kb

input:

5
4 7
9 8
32 84
11 35
2 1

output:

2
7
5
4
0

result:

ok 5 number(s): "2 7 5 4 0"

Test #2:

score: 0
Accepted
time: 683ms
memory: 24444kb

input:

300
223528529 446621399
755625805 978718675
257717538 480810408
277875777 54782907
963091558 739998688
282469431 505562301
38752451 261845321
474429597 697522467
67026335 290119205
874629705 651536835
301964126 78871256
316864911 93772041
545942587 322849717
640801578 417708708
683070559 906163429
9...

output:

12
19
15
17
16
17
13
19
15
17
15
15
18
17
17
19
14
18
15
16
17
18
19
15
17
17
17
12
19
16
12
19
16
15
13
18
17
18
16
15
14
17
14
16
15
14
19
18
16
14
16
17
16
18
14
16
17
14
18
17
16
19
19
18
17
16
16
14
23
14
14
18
18
20
16
16
17
17
12
18
19
17
16
18
16
15
18
12
15
18
16
12
19
18
15
18
17
14
13
16
...

result:

ok 300 numbers

Test #3:

score: 0
Accepted
time: 7ms
memory: 4832kb

input:

300
636099362 669653794
640628976 607074544
492031079 458476647
420324662 386770230
234845232 201290800
416098756 382544324
513320531 546874963
245050186 278604618
688926901 722481333
458533405 424978973
863331691 896886123
638892374 672446806
849746534 816192102
761591596 728037164
369934525 403488...

output:

51
49
48
45
35
45
50
42
53
47
61
53
59
55
43
41
53
55
44
62
48
39
36
60
40
55
49
54
50
38
59
41
39
43
47
57
54
50
52
39
54
52
39
37
32
34
56
38
53
41
51
50
55
56
35
44
35
45
59
33
53
59
56
57
43
53
54
45
34
58
33
55
56
33
46
49
33
38
38
55
54
47
56
47
52
60
36
53
56
53
44
43
34
59
55
49
52
40
35
55
...

result:

ok 300 numbers

Test #4:

score: -100
Time Limit Exceeded

input:

300
867787179 877654074
403881553 407432589
201172453 192754452
822066929 815320907
516801596 518734008
278735158 276997924
39842929 46406996
229923235 222281597
958111516 962220928
514454467 511546909
66273896 56535006
603599579 602886043
99119397 89783150
606236932 603774684
948362977 945928285
92...

output:


result: