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;
}
相关推荐
仰泳的熊猫1 小时前
LeetCode:538. 把二叉搜索树转换为累加树/1038. 从二叉搜索树到更大和树
数据结构·c++·算法·leetcode
weixin_307779131 小时前
Clickhouse导出库的表、视图、用户和角色定义的SQL语句
开发语言·数据库·算法·clickhouse·自动化
piggy侠1 小时前
【GitHub每日速递 251016】23k star,Daytona:90ms内极速运行AI代码,安全弹性基础设施来袭!
算法·github
小龙报1 小时前
《算法通关指南---C++编程篇(1)》
开发语言·c++·程序人生·算法·学习方法·visual studio
在繁华处1 小时前
C语言初步学习:数组的增删查改
c语言·数据结构·学习
月夜的风吹雨1 小时前
【数据结构】:C 语言常见排序算法的实现与特性解析
c语言·数据结构·排序算法
Cx330❀2 小时前
《C++ 手搓list容器底层》:从结构原理深度解析到功能实现(附源码版)
开发语言·数据结构·c++·经验分享·算法·list
Swift社区2 小时前
LeetCode 399 除法求值
算法·leetcode·职场和发展
仰泳的熊猫2 小时前
LeetCode:98. 验证二叉搜索树
数据结构·c++·算法·leetcode
Python智慧行囊2 小时前
图像处理(三)--开运算与闭运算,梯度运算,礼帽与黑帽
人工智能·算法·计算机视觉