来源: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
#include
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;
}
来源: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 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 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 Example 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. 题意 思路: 来源:http://codeforces.com/contest/1350/problem/C time limit per test3 seconds gcd(s) is the maximum positive integer x, such that all integers in s are divisible on x. Orac has a sequence a with length n. He come up with the multiset t={lcm({ai,aj}) | i Input The second line contains n integers, a1,a2,…,an (1≤ai≤200000). Output Examples For the second example, t={120,40,80,120,240,80}, and it’s not hard to see that gcd(t)=40. 题意: 和ai求lcm的数再求gcd即lcm(ai,gcd(ai+1,ai+2,…,an))因此预处理gcd(ai+1,ai+2,…,an)即可。
The first line contains one integer t (1≤t≤100): the number of queries.
Print t lines, the i-th of them should contain the maximum number of models that Orac can buy for the i-th query.
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.
给出n个数,找到满足条件的子序列的最大长度。
条件:
1.保证a[ i[j] ] 2.i[j]可以整除i[j+1]
令dp[i]dp[i]dp[i]表示以下标i结尾的最大长度。
有dp[j]=max(dp[j],dp[i]+1)(j∣i),然后取一遍最值
代码#include
C. Orac and LCM
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:
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.
The first line contains one integer n (2≤n≤100000).
Print one integer: gcd({lcm({ai,aj}) | i
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.
给定长度为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。#include