目录
- 题目描述
- 输入格式
- 输出格式
- 输入输出样例 #1
- 输入 #1
- 输出 #1
- 说明/提示
- 题目思路
- AC 代码
题目描述
You have been offered to play a game. In this game, there are $ n $ possible outcomes, and for each of them, you must bet a certain integer amount of coins. In the event that the $ i $ -th outcome turns out to be winning, you will receive back the amount of coins equal to your bet on that outcome, multiplied by $ k_i $ . Note that exactly one of the $ n $ outcomes will be winning.
Your task is to determine how to distribute the coins in such a way that you will come out ahead in the event of any winning outcome. More formally, the total amount of coins you bet on all outcomes must be strictly less than the number of coins received back for each possible winning outcome.
输入格式
Each test consists of multiple test cases. The first line contains a single integer $ t $ ( $ 1 \le t \le 10^4 $ ) — the number of test cases. The description of the test cases follows.
The first line of each test case contains a single integer $ n $ ( $ 1 \le n \le 50 $ ) — the number of outcomes.
The second line of each test case contains $ n $ integers $ k_1,k_2,\ldots,k_n $ ( $ 2 \le k_i \le 20 $ ) — the multiplier for the amount of coins if the $ i $ -th outcome turns out to be winning.
It is guaranteed that the sum of $ n $ over all test cases does not exceed $ 2 \cdot 10^5 $ .
输出格式
For each test case, output $ -1 $ if there is no way to distribute the coins as required. Otherwise, output $ n $ integers $ x_1, x_2,\ldots, x_n $ ( $ 1 \le x_i \le 10^{9} $ ) — your bets on the outcomes.
It can be shown that if a solution exists, there is always a solution that satisfies these constraints.
If there are multiple suitable solutions, output any of them.
输入输出样例 #1
输入 #1
6
3
3 2 7
2
3 3
5
5 5 5 5 5
6
7 9 3 17 9 13
3
6 3 2
5
9 4 6 8 3
输出 #1
27 41 12
1 1
-1
1989 1547 4641 819 1547 1071
-1
8 18 12 9 24
说明/提示
In the first test case, the coins can be distributed as follows: $ 27 $ coins on the first outcome, $ 41 $ coins on the second outcome, $ 12 $ coins on the third outcome. Then the total amount of coins bet on all outcomes is $ 27 + 41 + 12 = 80 $ coins. If the first outcome turns out to be winning, you will receive back $ 3 \cdot 27 = 81 $ coins, if the second outcome turns out to be winning, you will receive back $ 2 \cdot 41 = 82 $ coins, if the third outcome turns out to be winning, you will receive back $ 7 \cdot 12 = 84 $ coins. All these values are strictly greater than $ 80 $ .
In the second test case, one way is to bet one coin on each of the outcomes.
题目思路
猜结论。
对 k 1 , k 2 , … , k n , k n k_1,k_2,\dots,k_n,k_n k1,k2,…,kn,kn,求最小公倍数,设此值为 x x x,则累加 x x x 分别除以 k 1 , k 2 , … , k n k_1,k_2,\dots,k_n k1,k2,…,kn 的和,如果此值大于 x x x,则无解。否则,给第每个位置分配 x k i \frac{x}{k_i} kix 数量的钱即可。
AC 代码
#include <bits/stdc++.h>
using namespace std;
long long a[55];
long long gcd(long long a,long long b){if(b == 0) return a;return gcd(b,a % b);
}
long long lcm(long long a,long long b){return a * b / gcd(a,b);
}
void solve(){int n;cin >> n;bool ok = 0;long long g = 0,sum = 0;cin >> a[1];g = a[1];for(int i = 2;i <= n;i++){cin >> a[i];g = lcm(g,a[i]);}for(int i = 1;i <= n;i++){sum += g / a[i];}for(int i = 1;i <= n;i++){if(g <= sum){cout << -1;ok = 1;break;}}if(!ok){for(int i = 1;i <= n;i++){cout << g / a[i] << " ";}}cout << endl;
}
int main(){int t;cin >> t; while(t--){solve();}return 0;
}
创作不易,白嫖不好,各位的支持和认可,就是我创作的最大动力,如果喜欢我的文章,给个关注吧!
冰焰狼 | 文
如果本篇博客有任何错误,请批评指教,不胜感激 !