A8930 | The Minimum Number of Variables
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
You've got a positive integer sequence $a_{1},a_{2},...,a_{n}$ . All numbers in the sequence are distinct. Let's fix the set of variables $b_{1},b_{2},...,b_{m}$ . Initially each variable $b_{i}$ $(1<=i<=m)$ contains the value of zero. Consider the following sequence, consisting of $n$ operations.
The first operation is assigning the value of $a_{1}$ to some variable $b_{x}$ $(1<=x<=m)$ . Each of the following $n-1$ operations is assigning to some variable $b_{y}$ the value that is equal to the sum of values that are stored in the variables $b_{i}$ and $b_{j}$ $(1<=i,j,y<=m)$ . At that, the value that is assigned on the $t$ -th operation, must equal $a_{t}$ . For each operation numbers $y,i,j$ are chosen anew.
Your task is to find the minimum number of variables $m$ , such that those variables can help you perform the described sequence of operations.
The first operation is assigning the value of $a_{1}$ to some variable $b_{x}$ $(1<=x<=m)$ . Each of the following $n-1$ operations is assigning to some variable $b_{y}$ the value that is equal to the sum of values that are stored in the variables $b_{i}$ and $b_{j}$ $(1<=i,j,y<=m)$ . At that, the value that is assigned on the $t$ -th operation, must equal $a_{t}$ . For each operation numbers $y,i,j$ are chosen anew.
Your task is to find the minimum number of variables $m$ , such that those variables can help you perform the described sequence of operations.
输入格式
The first line contains integer $n$ $(1<=n<=23)$ . The second line contains $n$ space-separated integers $a_{1},a_{2},...,a_{n}$ $(1<=a_{k}<=10^{9})$ .
It is guaranteed that all numbers in the sequence are distinct.
It is guaranteed that all numbers in the sequence are distinct.
输出格式
In a single line print a single number — the minimum number of variables $m$ , such that those variables can help you perform the described sequence of operations.
If you cannot perform the sequence of operations at any $m$ , print -1.
If you cannot perform the sequence of operations at any $m$ , print -1.
输入输出样例
输入 #1
5 1 2 3 6 8
输出 #1
2
输入 #2
3 3 6 5
输出 #2
-1
输入 #3
6 2 4 8 6 10 18
输出 #3
3
In the first sample, you can use two variables $b_{1}$ and $b_{2}$ to perform the following sequence of operations.
1. $b_{1}$ := $1$ ;
2. $b_{2}$ := $b_{1}+b_{1}$ ;
3. $b_{1}$ := $b_{1}+b_{2}$ ;
4. $b_{1}$ := $b_{1}+b_{1}$ ;
5. $b_{1}$ := $b_{1}+b_{2}$ .
1. $b_{1}$ := $1$ ;
2. $b_{2}$ := $b_{1}+b_{1}$ ;
3. $b_{1}$ := $b_{1}+b_{2}$ ;
4. $b_{1}$ := $b_{1}+b_{1}$ ;
5. $b_{1}$ := $b_{1}+b_{2}$ .
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted