2020百度之星初赛一 1003 Dec hdu6745 动态规划or记忆化搜索

Problem Description

初始有 a,ba, ba,b 两个正整数,每次可以从中选一个大于 1 的数减 1,最后两个都会减到 1,我们想知道在过程中两个数互质的次数最多是多少。
Input

第一行一个正整数 test(1≤test≤1000000)test(1 \le test \le 1000000)test(1≤test≤1000000) 表示数据组数。

接下来 test 行,每行两个正整数 a,b(1≤a,b≤1000)a, b(1 \le a, b \le 1000)a,b(1≤a,b≤1000)。
Output

对于每组数据,一行一个整数表示答案。
Sample Input

1
2 3

Sample Output

4

样例解释
2 3 -> 1 3 -> 1 2 -> 1 1

dp或者记搜,我只会后者

#define debug
#ifdef debug
#include <time.h>
#include "/home/majiao/mb.h"
#endif

#include <iostream>
#include <algorithm>
#include <vector>
#include <string.h>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <math.h>

#define MAXN ((int)1e5+7)
#define ll long long 
#define INF (0x7f7f7f7f)
#define fori(lef, rig) for(int i=lef; i<=rig; i++)
#define forj(lef, rig) for(int j=lef; j<=rig; j++)
#define fork(lef, rig) for(int k=lef; k<=rig; k++)
#define QAQ (0)

using namespace std;

#ifdef debug
#define show(x...)                                 \
	do {                                           \
		cout << "\033[31;1m " << #x << " -> ";     \
		err(x);                                    \
	} while (0) 
#else
#define show(x...)  
#endif

void err() { cout << "\033[39;0m" << endl; }
template<typename T, typename... A>
void err(T a, A... x) { cout << a << ' '; err(x...); }

namespace FastIO {

	char print_f[105];
	void read() { }
	void print() { putchar('\n'); }

	template <typename T, typename... T2>
		inline void read(T &x, T2 &... oth) {
			x = 0;
			char ch = getchar();
			ll f = 1;
			while (!isdigit(ch)) {
				if (ch == '-') f *= -1; 
				ch = getchar();
			}
			while (isdigit(ch)) {
				x = x * 10 + ch - 48;
				ch = getchar();
			}
			x *= f;
			read(oth...);
		}
	template <typename T>
		inline void put(T x) {
			if(x==0) { putchar('0'); putchar('\n'); return; }
			if(x<0) { putchar('-'); x = -x; }
			int num=0;
			char ch[128];
			while(x) ch[++num] = x % 10 + '0', x /= 10;
			while(num) putchar(ch[num--]);
			putchar('\n');
		}
}; // namespace FastIO
using FastIO::read;
using FastIO::put;

int n = 1024, m, Q, K, dp[1024][1024];

int mem(int a, int b) {
	if(~dp[a][b]) return dp[a][b];
	if(a == 1 || b == 1) return (dp[a][b] = max(a, b));
	dp[a][b] = mem(a-1, b) + (__gcd(a, b) == 1);
	dp[a][b] = max(dp[a][b], mem(a, b-1)+(1==__gcd(a, b)));
	return dp[a][b];
}

signed main() {
#ifdef debug
	freopen("test", "r", stdin);
	// freopen("out_main", "w", stdout);
	clock_t stime = clock();
#endif
	memset(dp, -1, sizeof(dp));
	read(Q);
	dp[1][1] = 1;
	mem(1023, 1023);
#if 0
	m = 20;
	for(int i=1; i<=m; i++) {
		for(int j=1; j<=m; j++)
			printf("%d ", dp[i][j]);
		printf("\n");
	}
#endif
	while(Q--) {
		int a, b;
		read(a, b);
		int ans = dp[a][b];
		put(ans);
	}













#ifdef debug
	clock_t etime = clock();
	printf("rum time: %lf 秒\n",(double) (etime-stime)/CLOCKS_PER_SEC);
#endif 
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值