-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
WangSenyuan
committed
May 29, 2024
1 parent
cd5586f
commit 17a04e8
Showing
6 changed files
with
468 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
Sasha decided to give his girlfriend the best handbag, but unfortunately for Sasha, it is very expensive. Therefore, Sasha wants to earn it. After looking at earning tips on the internet, he decided to go to the casino. | ||
|
||
Sasha knows that the casino operates under the following rules. If Sasha places a bet of 𝑦 | ||
coins (where 𝑦 | ||
is a positive integer), then in case of winning, he will receive 𝑦⋅𝑘 | ||
coins (i.e., his number of coins will increase by 𝑦⋅(𝑘−1) | ||
). And in case of losing, he will lose the entire bet amount (i.e., his number of coins will decrease by 𝑦 | ||
). | ||
|
||
Note that the bet amount must always be a positive (>0 | ||
) integer and cannot exceed Sasha's current number of coins. | ||
|
||
Sasha also knows that there is a promotion at the casino: he cannot lose more than 𝑥 | ||
times in a row. | ||
|
||
Initially, Sasha has 𝑎 | ||
coins. He wonders whether he can place bets such that he is guaranteed to win any number of coins. In other words, is it true that for any integer 𝑛 | ||
, Sasha can make bets so that for any outcome that does not contradict the rules described above, at some moment of time he will have at least 𝑛 | ||
coins. | ||
|
||
### ideas | ||
1. 可以假设sasha在x的循环结中处理,在前x-1次,它投入1元,然后在最后一次,投入所有的(cur - (x - 1)) * k | ||
2. 然后重复 | ||
3. 如果 y < x => false (永远都不到) | ||
4. y = x, 如果 k <= y, 同样永远也达不到 | ||
5. 否则有限次后,肯定能达到 | ||
6. 这个想法是不对的 | ||
7. 因为在这x次中的,任何一次,他都可能会赢 | ||
8. 如果这个时候,bet的是1,那么收益就是k - 1 元 | ||
9. 如果 k - 1 > x, 那么就可以得到无穷多的收益 (a > x) | ||
10. 但是 | ||
|
||
### solution | ||
|
||
Let's notice that the condition that we can achieve arbitrarily large values means that we need to guarantee at least a +1 | ||
to our coins. At the very first win. In this case, we can repeat this strategy indefinitely. | ||
|
||
Also, let's notice that if we have lost a total of 𝑧 | ||
before, then in the next round we need to bet 𝑦 | ||
such that 𝑦⋅(𝑘−1)>𝑧 | ||
, because otherwise the casino can give us a win. In this case, the condition of not losing more than 𝑥 | ||
times in a row will disappear, and we will end up in the negative. Therefore, the tactic is not optimal. | ||
|
||
Therefore, the solution is as follows: we bet 1 | ||
at first, then we bet the minimum number such that the win covers our loss. And if we have enough to make such a bet for 𝑥+1 | ||
, then the casino must end up in the negative, otherwise we cannot win. | ||
|
||
So the solution is in 𝑂(𝑥) | ||
time complexity, where we simply calculate these values in a loop. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
"fmt" | ||
"os" | ||
) | ||
|
||
func main() { | ||
reader := bufio.NewReader(os.Stdin) | ||
|
||
tc := readNum(reader) | ||
var buf bytes.Buffer | ||
|
||
for tc > 0 { | ||
tc-- | ||
k, x, a := readThreeNums(reader) | ||
res := solve(k, x, a) | ||
|
||
if res { | ||
buf.WriteString("YES\n") | ||
} else { | ||
buf.WriteString("NO\n") | ||
} | ||
} | ||
|
||
fmt.Print(buf.String()) | ||
} | ||
|
||
func readString(reader *bufio.Reader) string { | ||
s, _ := reader.ReadString('\n') | ||
for i := 0; i < len(s); i++ { | ||
if s[i] == '\n' || s[i] == '\r' { | ||
return s[:i] | ||
} | ||
} | ||
return s | ||
} | ||
|
||
func readNInt64s(reader *bufio.Reader, n int) []int64 { | ||
res := make([]int64, n) | ||
s, _ := reader.ReadBytes('\n') | ||
|
||
var pos int | ||
|
||
for i := 0; i < n; i++ { | ||
pos = readInt64(s, pos, &res[i]) + 1 | ||
} | ||
|
||
return res | ||
} | ||
|
||
func readInt64(bytes []byte, from int, val *int64) int { | ||
i := from | ||
var sign int64 = 1 | ||
if bytes[i] == '-' { | ||
sign = -1 | ||
i++ | ||
} | ||
var tmp int64 | ||
for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' { | ||
tmp = tmp*10 + int64(bytes[i]-'0') | ||
i++ | ||
} | ||
*val = tmp * sign | ||
return i | ||
} | ||
|
||
func readInt(bytes []byte, from int, val *int) int { | ||
i := from | ||
sign := 1 | ||
if bytes[i] == '-' { | ||
sign = -1 | ||
i++ | ||
} | ||
tmp := 0 | ||
for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' { | ||
tmp = tmp*10 + int(bytes[i]-'0') | ||
i++ | ||
} | ||
*val = tmp * sign | ||
return i | ||
} | ||
|
||
func readNum(reader *bufio.Reader) (a int) { | ||
bs, _ := reader.ReadBytes('\n') | ||
readInt(bs, 0, &a) | ||
return | ||
} | ||
|
||
func readTwoNums(reader *bufio.Reader) (a int, b int) { | ||
res := readNNums(reader, 2) | ||
a, b = res[0], res[1] | ||
return | ||
} | ||
|
||
func readThreeNums(reader *bufio.Reader) (a int, b int, c int) { | ||
res := readNNums(reader, 3) | ||
a, b, c = res[0], res[1], res[2] | ||
return | ||
} | ||
|
||
func readNNums(reader *bufio.Reader, n int) []int { | ||
res := make([]int, n) | ||
x := 0 | ||
bs, _ := reader.ReadBytes('\n') | ||
for i := 0; i < n; i++ { | ||
for x < len(bs) && (bs[x] < '0' || bs[x] > '9') && bs[x] != '-' { | ||
x++ | ||
} | ||
x = readInt(bs, x, &res[i]) | ||
} | ||
return res | ||
} | ||
|
||
func solve(k int, x int, a int) bool { | ||
z := 1 | ||
for i := 2; i <= x; i++ { | ||
// 已经lose 了 z个coins | ||
if z >= a { | ||
return false | ||
} | ||
// y * (k - 1) > z | ||
y := z / (k - 1) | ||
for y*(k-1) <= z { | ||
y++ | ||
} | ||
if y > (a - z) { | ||
return false | ||
} | ||
z += y | ||
} | ||
// 最后一次获胜了 | ||
return (a-z)*k > a | ||
} |
19 changes: 19 additions & 0 deletions
19
src/codeforces/set1/set19/set192/set1929/c/solution_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package main | ||
|
||
import "testing" | ||
|
||
func runSample(t *testing.T, k int, x int, a int, expect bool) { | ||
res := solve(k, x, a) | ||
|
||
if res != expect { | ||
t.Fatalf("Sample expect %t, but got %t", expect, res) | ||
} | ||
} | ||
|
||
func TestSample1(t *testing.T) { | ||
runSample(t, 3, 3, 6, false) | ||
} | ||
|
||
func TestSample2(t *testing.T) { | ||
runSample(t, 2, 1, 7, true) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
Sasha wants to take a walk with his girlfriend in the city. The city consists of 𝑛 | ||
intersections, numbered from 1 | ||
to 𝑛 | ||
. Some of them are connected by roads, and from any intersection, there is exactly one simple path† | ||
to any other intersection. In other words, the intersections and the roads between them form a tree. | ||
|
||
Some of the intersections are considered dangerous. Since it is unsafe to walk alone in the city, Sasha does not want to visit three or more dangerous intersections during the walk. | ||
|
||
Sasha calls a set of intersections good if the following condition is satisfied: | ||
|
||
If in the city only the intersections contained in this set are dangerous, then any simple path in the city contains no more than two dangerous intersections. | ||
However, Sasha does not know which intersections are dangerous, so he is interested in the number of different good sets of intersections in the city. Since this number can be very large, output it modulo 998244353 | ||
. | ||
|
||
† | ||
A simple path is a path that passes through each intersection at most once. | ||
|
||
### ideas | ||
1. 任何长度超过2的path,它们的(至少3个)节点不能同时在set中 | ||
2. 正难则反? | ||
3. 如果选中u,那么任何它子树中的和它外边的链接,通过它的两个节点都是不行的 | ||
4. (pow(2, n - cnt[u]) - 1)* (pow(2, cnt[u]) - 1) | ||
5. u中的也有同样的办法计算。 | ||
6. 但是会不会有重复的? | ||
7. 会有的,考虑u, v, 会在计算v的时候,把u的贡献也统计进去, 在计算u的时候也会 | ||
8. 那就计算good的? | ||
9. 以u为一个端点的,如果是u中的节点,那么只能选择一个,这样的有cnt[u] - 1 中 | ||
10. 每个可选/可不选,总数 = pow(2, cnt[u] - 1)中 | ||
11. 其中bad的 = pow(2, ....) * .... 这是是要排除掉的 | ||
12. got it | ||
13. |
Oops, something went wrong.