ACM训练联盟周赛 C题 Alice和Bob的Nim游戏

题目描述

众所周知,Alice和Bob非常喜欢博弈,而且Alice永远是先手,Bob永远是后手。

Alice和Bob面前有3堆石子,Alice和Bob每次轮流拿某堆石子中的若干个石子(不可以是0个),拿到所有石子中最后一个石子的人获胜。这是一个只有3堆石子的Nim游戏。

Bob错误的认为,三堆石子的Nim游戏只需要少的两堆的石子数量加起来等于多的那一堆,后手就一定会胜利。所以,Bob把三堆石子的数量分别设为 {k,4k,5k}(k>0)。

现在Alice想要知道,在k 小于 2^n 的时候,有多少种情况先手一定会获得胜利。

输入

一个整数n(1 \le n \le 2 \times 10^91≤n≤2×109)。

输出

输出先手胜利的可能情形数。答案对10^9+7109+7取模。

输出时每行末尾的多余空格,不影响答案正确性

样例输入

3

样例输出

2

题目来源

ACM训练联盟周赛

Nim博弈论,当先手为K^(4K)^(5K) == 0时必输。要求先手必赢,让总的数量减去必输的数量就是答案了。当K^(4K)^(5K) == 0时,K^(4K) == 5K,又因为K+2K=5K,所以K的二进制相隔一个位置不能同时为1.

则f(n) = f(n-1) + f(n-3) + f(n-4) + 2,总的数量为2n-1。

有了公式用矩阵快速幂就可以求出来了。

 1 #include <bits/stdc++.h>
 2 #define ll long long
 3 using namespace std;
 4 const ll mod = 1e9+7;
 5 struct mat{
 6     ll m[5][5];
 7     mat() {
 8         memset(m, 0, sizeof(m));
 9     }
10 };
11
12 mat mul(mat &A, mat &B) {
13     mat C;
14     for(int i = 0; i < 5; i ++) {
15         for(int j = 0; j < 5; j ++) {
16             for(int k = 0; k < 5; k ++) {
17                 C.m[i][j] = (C.m[i][j] + A.m[i][k]*B.m[k][j]) % mod;
18             }
19         }
20     }
21     return C;
22 }
23
24 mat pow(mat A, ll n) {
25     mat B;
26     for(int i = 0; i < 5; i ++) B.m[i][i] = 1;
27     while(n) {
28         if(n&1) B = mul(B,A);
29         A = mul(A,A);
30         n >>= 1;
31     }
32     return B;
33 }
34 ll pow(ll n) {
35     ll x = 2, y = 1;
36     while(n) {
37         if(n&1) y = (y*x)%mod;
38         x = x*x %mod;
39         n >>= 1;
40     }
41     return y;
42 }
43 int main() {
44     ll n;
45     cin >> n;
46     if(n <= 2) return 0*printf("0\n");
47     else if(n == 3) return 0*printf("2\n");
48     else if(n == 4) return 0*printf("7\n");
49     mat A;
50     A.m[0][0] = A.m[0][2] = A.m[0][3] = A.m[0][4] = 1;
51     A.m[1][0] = A.m[2][1] = A.m[3][2] = A.m[4][4] = 1;
52     A = pow(A, n-4);
53     ll ans = (A.m[0][0]*8+A.m[0][1]*5+A.m[0][2]*3+A.m[0][3]+A.m[0][4]*2+mod)%mod;
54     ans = (pow(n) - ans - 1 + mod) %mod;
55     printf("%lld\n",ans);
56     return 0;
57 }
58
59 // fn    1 0 1 1 1     fn-1
60 // fn-1  1 0 0 0 0     fn-2
61 // fn-2  0 1 0 0 0     fn-3
62 // fn-3  0 0 1 0 0     fn-4
63 //   2   0 0 0 0 1       2

原文地址:https://www.cnblogs.com/xingkongyihao/p/9315128.html

时间: 2024-08-04 16:16:05

ACM训练联盟周赛 C题 Alice和Bob的Nim游戏的相关文章

2020-3-14 acm训练联盟周赛Preliminaries for Benelux Algorithm Programming Contest 2019 解题报告+补题报告

2020-3-15比赛解题报告+2020-3-8—2020-3-15的补题报告 2020-3-15比赛题解 训练联盟周赛Preliminaries for Benelux Algorithm Programming Contest 2019  A建筑(模拟) 耗时:3ms 244KB 建筑 你哥哥在最近的建筑问题突破大会上获得了一个奖项 并获得了千载难逢的重新设计城市中心的机会 他最喜欢的城市奈梅根.由于城市布局中最引人注目的部分是天际线, 你的兄弟已经开始为他想要北方和东方的天际线画一些想法

[ICPC训练联盟周赛1] CTU Open Contest 2019

昨天ICPC训练联盟进行了第一场比赛,题目是CTU Open Contest 2019,烟台大学给出了解析. 题目和题解在此(提取码zre3) 我和索队还有yz组队,喊着队友nb就AK了,抱大腿真爽. A题是简单的组合数学,显然有公式 \(1\times C_n^1+2\times C_n^2+ \dots + n \times C_n^n=n \times 2^{n-1}\) 但忘了考虑\(n=0\)的情况,我wa了一发. B题题目我不太读得懂,yz A的. C题是算圆和矩形相交的面积,我抄了

博弈问题-Alice与Bob拿牌游戏

Description Bob and Alice play a game, and Bob will play first. Here is the rule of the game: 1) There are N stones at first; 2) Bob and Alice take turns to remove stones. Each time, they can remove p^k stones. p is prime number, such as 2, 3, 5, ...

2020.3.28-ICPC训练联盟周赛,选用试题:UCF Local Programming Contest 2016

A.Majestic 10 签到题. #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<bitset> #include<cassert> #include<cctype> #include<cmath> #include<cstdlib> #include<ctime>

03.28,周六,12:00-17:00,ICPC训练联盟周赛,选用试题:UCF Local Programming Contest 2016正式赛。

A Majestic 10 就是判断比十大的数字的个数.签到提 #include<bits/stdc++.h> using namespace std; int main() { int n; cin>>n; while(n--) { int cnt = 0,a[3]; cin>>a[0]>>a[1]>>a[2]; int i; for(i = 0;i < 3;i++) { if(a[i]>=10) cnt++; } cout<

2011 ACM-ICPC 成都赛区A题 Alice and Bob (博弈动规)

题目大意: 有K堆石子,每堆有Ki个.两人的操作能够是: 1 从某一堆拿走一个 假设该堆在此之后没有石子了.就消失 2 合并两个堆 求是否先手必胜,先手胜输出Alice.否则输出Bob 思路: 这道题读完后毫无头绪.推了半天也推不个所以然来,參看大神代码后,感觉就是一个记忆化搜索啊,唉,知识学多了不会用还是白搭.还得多做题啊! 这里我们把数字分成 1,2,大于等于3的奇数,大于等于4的偶数四类. 这样分的原因在于.一个大于3的奇数是实际上等价于3的:由于每当对手减一个.自己也减一个.就又变回了一

Sdut 2108 Alice and Bob(数学题)(山东省ACM第四届省赛D题)

题目地址:sdut 2608 Alice and Bob Alice and Bob Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 Alice and Bob like playing games very much.Today, they introduce a new game. There is a polynomial like this: (a0*x^(2^0)+1) * (a1 * x^(2^1)+1)*....

2013 ACM/ICPC 长沙现场赛 A题 - Alice&#39;s Print Service (ZOJ 3726)

Alice's Print Service Time Limit: 2 Seconds      Memory Limit: 65536 KB Alice is providing print service, while the pricing doesn't seem to be reasonable, so people using her print service found some tricks to save money. For example, the price when

ACM训练二A题

这个题目很简单,虽然看上去高大上,只要找规律就行了,多看看,找找例子.-不过我花了蛮久才看出来,惭愧啊,其实就是看n.m中最小的数是奇数还是偶数. After winning gold and silver in IOI 2014, Akshat and Malvika want to have some fun. Now they are playing a game on a grid made of nhorizontal and m vertical sticks. An interse