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

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;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值