Codeforces Round 943 (Div. 3)补题

A. Maximize?

题意:

给定一个整数 xx 。您的任务是找到任意整数 yy (1≤y<x)(1≤y<x) ,使得 gcd(x,y)+ygcd(x,y)+y 为最大可能值 请注意,如果有多个 yy 满足该语句,您可以找到任意一个。

gcd(a,b)gcd(a,b) 是 aa 和 bb 的最大公约数。例如, gcd(6,4)=2gcd(6,4)=2

思路 :

暴力枚举

cpp 复制代码
#include <iostream>
#include<algorithm>
#include<numeric>
#define int long long
#define endl '\n'
using namespace std;
typedef pair<int, int> PII;
int gcd(int a, int b)
{
	return b ? gcd(b, a % b) : a;
}

void solve() 
{
	int x; cin >> x;
	int res = 0, id = 1;
	for (int y = 1; y < x; y++) 
	{
		if (gcd(x, y) + y > res) 
		{
			res = gcd(x, y) + y;
			id = y;
		}
	}
	cout << id << endl;
}
signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	int tt; cin >> tt;
	while (tt--) solve();
	return 0;
}

B. Prefiquence

题意:

思路:

按顺序枚举 a aa 的每个字符,找到 b bb 串中可以与它对应的字符,对应起来。模拟一遍这个过程即可知道 a aa 最多能对应到长度为几的前缀了。

cpp 复制代码
#include <iostream>
#define int long long
#define endl '\n'
using namespace std;
typedef pair<int, int> PII;
void solve() {
    int n, m; cin >> n >> m;
    int j = 0, ans = 0;
    string a, b; cin >> a >> b;
    for (int i = 0; i < n; i++) 
    {
        while (j < m && b[j] != a[i]) j += 1;
        if (j == m) break;
        ans += 1, j += 1;
    }
    cout << ans << endl;
}
signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int tt; cin >> tt;
    while (tt--) solve();
    return 0;
}

C. Assembly via Remainders

题意:

思路:

cpp 复制代码
#include<iostream>
#include<cstdio>
using namespace std;
const int N = 505;
int t, n;
int a[N], x[N];

int main()
{
	cin >> t;
	while (t--)
	{
		cin >> n;
		for (int i = 2; i <= n; i++) cin >> x[i];
		a[n] = 1e9;
		for (int i = n - 1; i >= 1; i--)
		{
			a[i] = a[i + 1] - x[i + 1];
		}
		for (int i = 1; i <= n; i++) cout << a[i] << " ";
	}
	return 0;
}
相关推荐
j_xxx404_2 小时前
数据结构:栈和队列力扣算法题
c语言·数据结构·算法·leetcode·链表
南莺莺2 小时前
假设一个算术表达式中包含圆括号、方括号和花括号3种类型的括号,编写一个算法来判别,表达式中的括号是否配对,以字符“\0“作为算术表达式的结束符
c语言·数据结构·算法·
THMAIL2 小时前
深度学习从入门到精通 - 神经网络核心原理:从生物神经元到数学模型蜕变
人工智能·python·深度学习·神经网络·算法·机器学习·逻辑回归
野犬寒鸦2 小时前
力扣hot100:旋转图像(48)(详细图解以及核心思路剖析)
java·数据结构·后端·算法·leetcode
墨染点香2 小时前
LeetCode 刷题【61. 旋转链表】
算法·leetcode·职场和发展
一枝小雨2 小时前
【OJ】C++ vector类OJ题
数据结构·c++·算法·leetcode·oj题
Tisfy3 小时前
LeetCode 3516.找到最近的人:计算绝对值大小
数学·算法·leetcode·题解
自信的小螺丝钉3 小时前
Leetcode 206. 反转链表 迭代/递归
算法·leetcode·链表
黑色的山岗在沉睡3 小时前
LeetCode 189. 轮转数组
java·算法·leetcode
墨染点香3 小时前
LeetCode 刷题【65. 有效数字】
算法·leetcode·职场和发展