It's holiday. Mashmokh and his boss, Bimokh, are playing a game invented by Mashmokh.
In this game Mashmokh writes sequence of n distinct integers on the board. Then Bimokh makes several (possibly zero) moves. On the first move he removes the first and the second integer from from the board, on the second move he removes the first and the second integer of the remaining sequence from the board, and so on. Bimokh stops when the board contains less than two numbers. When Bimokh removes numbers x and y from the board, he gets gcd(x, y) points. At the beginning of the game Bimokh has zero points.
Mashmokh wants to win in the game. For this reason he wants his boss to get exactly k points in total. But the guy doesn't know how choose the initial sequence in the right way.
Please, help him. Find n distinct integers a1, a2, ..., an such that his boss will score exactly k points. Also Mashmokh can't memorize too huge numbers. Therefore each of these integers must be at most 109.
The first line of input contains two space-separated integers n, k (1 ≤ n ≤ 105; 0 ≤ k ≤ 108).
If such sequence doesn't exist output -1 otherwise output n distinct space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109).
5 2
1 2 3 4 5
5 3
2 4 3 7 1
7 2
-1
gcd(x, y) is greatest common divisor of x and y.
一个长度为n的数列,每次取前两个数的最大公约数累加后抛弃这两个数,直到这个数列中不足两个数为止。
思路:
因为0< k <1e8,而0< ai < 1e9,所以数列中前两个数可以为k-n/2+1和2 * (k - n / 2 + 1),而后面的数都是2*(k-n/2+1)+n,n=1,2,3…,这样这些数的最大公约数就是1,加上前两个数的结果就是k。这个题尤其需要注意的是几种特殊情况,如n = 1, k = 0等。
AC代码:
#include<iostream>
#include<cstdio>
using namespace std;
main()
{
int n, k;
while(cin >> n >> k)
{
if(k < n / 2)
{
puts("-1");
continue;
}
if(k == 0 && n == 1)
{
puts("1");
continue;
}
else if(n == 1)
{
puts("-1");
continue;
}
int k1 = k - n / 2 + 1;
if(k1 <= 0)
k1 = 1;
int k2 = 2 * k1, cnt = 1;
for(int i = 0; i < n / 2; i++)
{
if(i == 0)
printf("%d %d ", k1, k2);
else
{
printf("%d %d ", k2 + cnt, k2 + cnt + 1);
cnt += 2;
}
}
if(n % 2)
printf("%d", k2 + cnt);
printf("\n");
}
}
Mashmokh's boss, Bimokh, didn't like Mashmokh. So he fired him. Mashmokh decided to go to university and participate in ACM instead of finding a new job. He wants to become a member of Bamokh's team. In order to join he was given some programming tasks and one week to solve them. Mashmokh is not a very experienced programmer. Actually he is not a programmer at all. So he wasn't able to solve them. That's why he asked you to help him with these tasks. One of these tasks is the following.
A sequence of l integers b1, b2, ..., bl (1 ≤ b1 ≤ b2 ≤ ... ≤ bl ≤ n) is
called good if each number divides (without a remainder) by the next number in the sequence. More formally for
all i (1 ≤ i ≤ l - 1).
Given n and k find the number of good sequences of length k. As the answer can be rather large print it modulo 1000000007 (109 + 7).
The first line of input contains two space-separated integers n, k (1 ≤ n, k ≤ 2000).
Output a single integer — the number of good sequences of length k modulo 1000000007 (109 + 7).
3 2
5
6 4
39
2 1
2
In the first sample the good sequences are: [1, 1], [2, 2], [3, 3], [1, 2], [1, 3].
输入n和k,n为数列的最大值,k为数列长度,问有多少种数列满足1 ≤ b1 ≤ b2 ≤ ... ≤ bl ≤ n且前面的数是后面的约数(即能被后面的数整除)。结果要整除1e9+7。
一看题目就知道是典型的dp,但是我还是只菜鸡,即使知道是dp也不知道怎么写,后来学习了别人的代码终于知道怎么回事了。
思路:
建立一个dp[i][j]数组,i是长度,j是最大值。dp[i][j]中存的是相对dp[i][j - 1]的结果(注意是结果)的变化量,真正的结果是ans[i][j],ans[i][j]=dp[i][1] + ... +dp[i][j]。
状态转移方程为dp[j + 1][p] += dp[j][i],p为i的整倍数。
方程推导过程:比如dp[2][4] = dp[1][1] + dp[1][2] + dp[1][4],dp[1][1]是[1],在dp[2][4]里变成了[1,4],而dp[1][2]的[2]成了[2, 4], dp[1][4]的[4]成了[4,4]。
AC代码:
#include<iostream>
using namespace std;
const int M = 2020;
const int MOD = 1e9 + 7;
int n, l, dp[M][M];
main()
{
cin >> n >> l;
dp[0][1] = 1;
for(int i = 0; i < l; i++)
for(int j = 1; j <= n; j++)
for(int k = j; k <= n; k+= j)
dp[i + 1][k] = (dp[i + 1][k] + dp[i][j]) % MOD;
long long res = 0;
for(int i = 1; i <= n; i++)
res += dp[l][i] % MOD;
cout << dp[2][4] <<endl;
cout << dp[1][1] << " " << dp[1][2] << dp[1][4] << endl;
cout << res % MOD << endl;
}