问题 C: Coprime Set
时间限制: 1 Sec 内存限制: 128 MB Special Judge
题目描述
Given is a positive integer N. Print an integer sequence A=(A1,A2,…,AN) satisfying all of the following:
1≤Ai≤10000;
Ai≠Aj and gcd(Ai,Aj)>1 for i≠j;
gcd(A1,A2,…,AN)=1.
We can prove that, under the Constraints of this problem, such an integer sequence always exists.
Constraints
3≤N≤2500
输入
Input is given from Standard Input in the following format:
N
输出
Print the elements in your integer sequence A satisfying the conditions in one line, with spaces in between.
A1 A2 … AN
If multiple sequences satisfy the conditions, any of them will be accepted.
样例输入
4
样例输出
84 60 105 70
提示
All of the conditions are satisfied, since we have:
gcd(84,60)=12
gcd(84,105)=21
gcd(84,70)=14
gcd(60,105)=15
gcd(60,70)=10
gcd(105,70)=35
gcd(84,60,105,70)=1
唯一会的一道数论。
gcd(1,Z)=1 (除了0),1的因数只有1,使得gcd(all) 恒为1。
钻了数据的空,未考虑任意gcd(Ai,Aj)>1(别学我)。
#include<iostream>
using namespace std;
int main() {
int n;
cin >> n;
while (n--) cout << n + 1 << ' ';
return 0;
}
问题 I: 外卖
时间限制: 1 Sec 内存限制: 128 MB
题目描述
Bob 是一个重度外卖依赖者。这天他挑中了一家店,这家店总共有 n 种菜品,每种菜品限点一份,需要满 m 元钱才可配送,因此 Bob 想知道他至少需要花多少钱才能满足最低配送要求。
输入
输入共两行,第一行为两个正整数,n 和 m,第二行为 n 个正整数 ai
输出
输出一个数,满足最低配送要求所花的最少钱数。
样例输入
【样例1】
3 10
3 7 9
【样例2】
5 12
10 11 7 8 9
【样例3】
3 8
1 6 9
样例输出
【样例1】
10
【样例2】
15
【样例3】
9
提示
对于第二个样例,最低配送要求为 12 元,最优解为点 7 块和 8 块的两个菜,最少花 15元。
对于 30% 的数据,满足 n ≤ 15 。
对于 100% 的数据,满足 n ≤ 200, m ≤所有ai的和 ≤ 50000 。
有n个物品,每个物品拥有代价值,从n个物品中选出一种总代价最小的(sum>=m)。01背包,只不过本题里的体积(重量)vol与价值val都是同一个,且递推顺序和转移方程要换下。
tol =val [i]。
用dp [n][tol] 记录。特别的,除0号列外初始化为INF。满m配送,若 val [i] < m,那么不存在一种方法(cost)满足条件,即应付出的代价无穷大;若m为0,那么只要买0元的东西便可配送(0元购)。
首先打个表,以样例1为例:
ll dp[n][tol];
memset(dp, INF1, sizeof(dp));
_rep(i, 0, n) dp[i][0] = 0;
_rep(i, 1, n) {
_re_rep(j, tol, val[i]) dp[i][j] = min(dp[i - 1][j], dp[i - 1][j - val[i]] + val[i]);
_re_rep(j, val[i] - 1, 1) dp[i][j] = min(dp[i][j + 1], dp[i - 1][j]);
}
cout << dp[n][m];
空间优化一下:
#include<iostream>
#include<bits/stdc++.h>
#include<algorithm>
#include<string>
#include<stack>
#include<map>
#include<list>
#include<vector>
#include<queue>
#include<deque>
#include<set>
#include<functional>
#include<limits.h>
#include<float.h>
#define pi 3.14159265358979323846264338327950288419716939937510582097494
using namespace std;
#define _for(i, a, b) for(int i=(a);i<(b);++i)
#define _rep(i, a, b) for(int i=(a);i<=(b);++i)
#define _re_for(i, a, b) for(int i=(a);i>(b);--i)
#define _re_rep(i, a, b) for(int i=(a);i>=(b);--i)
typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
const int INF1 = 0x3f3f3f3f;
const int INF0 = 0xc0c0c0c0;
// INT_MAX INT_MIN
// DBL_MAX DBL_MIN
ll read() {
ll x = 0, f = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') f = -1;
c = getchar();
}
while (isdigit(c)) x = x * 10 + (c - 48), c = getchar();
return x * f;
}
ll n, m;
ll dp[50050], val[220], tol;
int main() {
n = read();
m = read();
_rep(i, 1, n) val[i] = read(), tol += val[i];
memset(dp, INF1, sizeof(dp));
dp[0] = 0;
_rep(i, 1, n) {
_re_rep(j, tol, val[i]) dp[j] = min(dp[j], dp[j - val[i]] + val[i]);
_re_rep(j, val[i] - 1, 1) dp[j] = min(dp[j], dp[j + 1]);
}
cout << dp[m];
return 0;
}