Codeforces Round #641 (Div. 2)题目+题解(A、B、C)

本文解析了CodeForces平台上的三道算法题,包括寻找最小非平凡因子、构建美丽模型序列以及计算序列元素两两组合的最小公倍数的集合的最大公约数。通过数论方法和动态规划解决复杂问题。

A. Orac and Factors

来源:http://codeforces.com/contest/1350/problem/A

time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Orac is studying number theory, and he is interested in the properties of divisors.

For two positive integers a and b, a is a divisor of b if and only if there exists an integer c, such that a⋅c=b.

For n≥2, we will denote as f(n) the smallest positive divisor of n, except 1.

For example, f(7)=7,f(10)=2,f(35)=5.

For the fixed integer n, Orac decided to add f(n) to n.

For example, if he had an integer n=5, the new value of n will be equal to 10. And if he had an integer n=6, n will be changed to 8.

Orac loved it so much, so he decided to repeat this operation several times.

Now, for two positive integers n and k, Orac asked you to add f(n) to n exactly k times (note that n will change after each operation, so f(n) may change too) and tell him the final value of n.

For example, if Orac gives you n=5 and k=2, at first you should add f(5)=5 to n=5, so your new value of n will be equal to n=10, after that, you should add f(10)=2 to 10, so your new (and the final!) value of n will be equal to 12.

Orac may ask you these queries many times.

Input
The first line of the input is a single integer t (1≤t≤100): the number of times that Orac will ask you.

Each of the next t lines contains two positive integers n,k (2≤n≤106,1≤k≤109), corresponding to a query by Orac.

It is guaranteed that the total sum of n is at most 106.

Output
Print t lines, the i-th of them should contain the final value of n in the i-th query by Orac.

Example
inputCopy
3
5 1
8 2
3 4
outputCopy
10
12
12
Note
In the first query, n=5 and k=1. The divisors of 5 are 1 and 5, the smallest one except 1 is 5. Therefore, the only operation adds f(5)=5 to 5, and the result is 10.

In the second query, n=8 and k=2. The divisors of 8 are 1,2,4,8, where the smallest one except 1 is 2, then after one operation 8 turns into 8+(f(8)=2)=10. The divisors of 10 are 1,2,5,10, where the smallest one except 1 is 2, therefore the answer is 10+(f(10)=2)=12.

In the third query, n is changed as follows: 3→6→8→10→12.

题意:
定义 f(n) 为 n 的最小非平凡因子,也就是除了 1,n 之外的最小因子
给出两个正整数 n,k,你需要进行 k次操作,每次将 n 加上 f(n)(注意 n在每次操作后是会变化的)
思路:
数论的题目

第一次找约数,
第二遍,开始直接+2即可。
代码

#include <cstdio>
#include<iostream>
int main()
{
	int t,n,k,i;
	scanf("%d",&t);
	while(t--){
		scanf("%d%d",&n,&k);
		for(i=2;i<=n;i++)
			if(n%i==0)break;
		n+=i,k--;
		n+=k*2;
		printf("%d\n",n);
	}
	return 0;
}

B. Orac and Models

来源:http://codeforces.com/contest/1350/problem/B

time limit per test3 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
There are n models in the shop numbered from 1 to n, with sizes s1,s2,…,sn.

Orac will buy some of the models and will arrange them in the order of increasing numbers (i.e. indices, but not sizes).

Orac thinks that the obtained arrangement is beatiful, if for any two adjacent models with indices ij and ij+1 (note that ij<ij+1, because Orac arranged them properly), ij+1 is divisible by ij and sij<sij+1.

For example, for 6 models with sizes {3,6,7,7,7,7}, he can buy models with indices 1, 2, and 6, and the obtained arrangement will be beautiful. Also, note that the arrangement with exactly one model is also considered beautiful.

Orac wants to know the maximum number of models that he can buy, and he may ask you these queries many times.

Input
The first line contains one integer t (1≤t≤100): the number of queries.

Each query contains two lines. The first line contains one integer n (1≤n≤100000): the number of models in the shop, and the second line contains n integers s1,…,sn (1≤si≤109): the sizes of models.

It is guaranteed that the total sum of n is at most 100000.

Output
Print t lines, the i-th of them should contain the maximum number of models that Orac can buy for the i-th query.

Example
inputCopy
4
4
5 3 4 6
7
1 4 2 3 6 4 9
5
5 4 3 2 1
1
9
outputCopy
2
3
1
1
Note
In the first query, for example, Orac can buy models with indices 2 and 4, the arrangement will be beautiful because 4 is divisible by 2 and 6 is more than 3. By enumerating, we can easily find that there are no beautiful arrangements with more than two models.

In the second query, Orac can buy models with indices 1, 3, and 6. By enumerating, we can easily find that there are no beautiful arrangements with more than three models.

In the third query, there are no beautiful arrangements with more than one model.

题意
给出n个数,找到满足条件的子序列的最大长度。
条件:
1.保证a[ i[j] ]<a[ i[j+1] ]
2.i[j]可以整除i[j+1]

思路:
令dp[i]dp[i]dp[i]表示以下标i结尾的最大长度。
有dp[j]=max(dp[j],dp[i]+1)(j∣i),然后取一遍最值
代码

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
int f[N],a[N],n;
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
	scanf("%d",&n);
	for(int i=1;i<=n;i++) 
	scanf("%d",&a[i]),f[i]=1;
	for(int i=1;i<=n;i++)
		for(int j=i*2;j<=n;j+=i)
		{  
			if(a[j]>a[i]) f[j]=max(f[j],f[i]+1);
		}
	int ans=1;
	for(int i=1;i<=n;i++)
	 ans=max(f[i],ans);
	printf("%d\n",ans);
	}
	return 0;
} 

C. Orac and LCM

来源:http://codeforces.com/contest/1350/problem/C

time limit per test3 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
For the multiset of positive integers s={s1,s2,…,sk}, define the Greatest Common Divisor (GCD) and Least Common Multiple (LCM) of s as follow:

gcd(s) is the maximum positive integer x, such that all integers in s are divisible on x.
lcm(s) is the minimum positive integer x, that divisible on all integers from s.
For example, gcd({8,12})=4,gcd({12,18,6})=6 and lcm({4,6})=12. Note that for any positive integer x, gcd({x})=lcm({x})=x.

Orac has a sequence a with length n. He come up with the multiset t={lcm({ai,aj}) | i<j}, and asked you to find the value of gcd(t) for him. In other words, you need to calculate the GCD of LCMs of all pairs of elements in the given sequence.

Input
The first line contains one integer n (2≤n≤100000).

The second line contains n integers, a1,a2,…,an (1≤ai≤200000).

Output
Print one integer: gcd({lcm({ai,aj}) | i<j}).

Examples
inputCopy
2
1 1
outputCopy
1
inputCopy
4
10 24 40 80
outputCopy
40
inputCopy
10
540 648 810 648 720 540 594 864 972 648
outputCopy
54
Note
For the first example, t={lcm({1,1})}={1}, so gcd(t)=1.

For the second example, t={120,40,80,120,240,80}, and it’s not hard to see that gcd(t)=40.

题意:
给定长度为n的序列,对每两个数取lcm,得到n×(n−1)/2个lcm,再求这n×(n−1)/2个lcm的gcd
思路:
还是数论鸭
观察可得:
对于每个数分解质因数,如果至少两个数中不含质因子ppp,则ppp不会出现在答案中。设ai和aj中不存在p,那么对于ai和aj构成的lcm中也不含p。故答案中不存在p。故若p对答案有贡献,必然至少n−1个数中有p。

和ai求lcm的数再求gcd即lcm(ai,gcd(ai+1,ai+2,…,an))因此预处理gcd(ai+1,ai+2,…,an)即可。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 10;
int q[N], n;
ll p[N];
ll gcd(ll a, ll b) 
{return b == 0 ? a : gcd(b, a % b);} 
ll lcm(ll a, ll b) 
{return a / gcd(a, b) * b;} 
int main()
{
	scanf("%d", &n);
	for(int i = 1; i <= n; i++) scanf("%d", &q[i]);
	p[n] = q[n];
	for(int i = n - 1; i >= 1; i--) p[i] = gcd(p[i + 1], q[i]);
	ll res = lcm(q[1], p[2]);
	for(int i = 2; i < n; i++) res = gcd(res, lcm(q[i], p[i + 1]));
	printf("%lld\n", res);
	
	return 0;
}

Codeforces Round 1036 是一场同时面向 Div.1 和 Div.2 参赛者的比赛,通常这类比赛会包含多个具有挑战性的编程题目,涵盖算法、数据结构、数学等多个领域。比赛的题解题目信息可以帮助参赛者回顾解题思路,提升编程能力。 ### 比赛基本信息 - **比赛名称**:Codeforces Round #1036 (Div. 1 and Div. 2) - **比赛时间**:具体时间为 UTC+X(根据实际举办日期和时间表) - **比赛链接**:[Codeforces 官方页面](https://codeforces.com/contest/1343) - **题解发布位置**:通常在比赛结束后不久,官方或社区成员会在 Codeforces 博客、GitHub 或其他技术平台上发布题解### 题目类型与难度分布 该轮比赛通常包括 5 到 7 道题目,难度从简单实现到复杂算法不等。例如: - **A题**:通常是简单的模拟或数学问题。 - **B题**:可能涉及字符串处理或基础贪心策略。 - **C题**:中等难度,可能需要掌握基本的数据结构如数组、排序等。 - **D题及以后**:较高难度,可能涉及图论、动态规划、数论等高级算法。 ### 参赛情况与亮点 - **参与人数**:通常超过 10,000 名选手参加。 - **热门话题**:比赛中某些题目可能会引发广泛讨论,尤其是那些需要用到巧妙构造或优化技巧的问题。 - **知名选手表现**:顶尖选手如 tourist、Um_nik 等通常会以极快的速度完成所有题目,并占据排行榜前列。 ### 示例代码片段 以下是一个典型的 Codeforces 题目解法示例,适用于某道中等难度题目: ```cpp #include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while(t--) { long long l, r; cin >> l >> r; // 假设 e 是一个预处理好的符合条件的数组 // 使用二分查找来统计区间 [l, r] 内的有效数字个数 long long ans = upper_bound(e.begin(), e.end(), r) - lower_bound(e.begin(), e.end(), l); cout << ans << endl; } return 0; } ``` ### 题解资源推荐 - **Codeforces 官方博客**:通常会有详细的题解和作者说明。 - **GitHub 仓库**:许多参赛者会将自己的解法上传至 GitHub,便于他人学习。 - **知乎专栏 / 优快云 / 博客园**:中文社区中也常有高质量的赛后总结与分析文章。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值