【SRM】518 Nim
题意
\(K(1 \le K \le 10^9)\)堆石子,每堆石子个数不超过\(L(2 \le 50000)\),问Nim游戏中先手必败局面的数量,答案对\(10^9+7\)取模。
分析
容易得到\(f(i, k) = \sum_{j=0}^{n-1} f(i-1, j) f(i-1, k^j), f(1, i(2 \le i \le L))=1\),其中\(n=min(2^i, 2^i > L)\)。发现其实这就是操作为\(xor\)的卷积。于是用鬼畜的fwt做就行了。
题解
然后fwt+快速幂即可。
// BEGIN CUT HERE
// END CUT HERE
#line 5 "Nim.cpp"
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
const int mo=1e9+7, N=100005, two=(1e9+8)/2;
void fwt(int *a, int l, int r, int f) {
if(r-l==1) {
return;
}
int mid=(l+r)>>1;
if(!f) {
fwt(a, l, mid, f);
fwt(a, mid, r, f);
}
int g=f?two:1;
for(int i=l, m=(r-l)>>1; i<mid; ++i) {
int x=a[i], y=a[i+m];
a[i]=(ll)(x+y)%mo*g%mo;
a[i+m]=(ll)(x-y+mo)%mo*g%mo;
}
if(f) {
fwt(a, l, mid, f);
fwt(a, mid, r, f);
}
}
int ipow(int a, int b) {
int x=1;
for(; b; b>>=1, a=(ll)a*a%mo) {
if(b&1) {
x=(ll)x*a%mo;
}
}
return x;
}
int a[N];
class Nim {
public:
int count(int K, int L) {
int len=1;
for(; len<=L; len<<=1);
memset(a, 0, sizeof(int)*len);
for(int i=2; i<=L; ++i) {
a[i]=1;
}
for(int i=2; i<=L; ++i) {
if(a[i]) {
for(int j=i+i; j<=L; j+=i) {
a[j]=0;
}
}
}
fwt(a, 0, len, 0);
for(int i=0; i<len; ++i) {
a[i]=ipow(a[i], K);
}
fwt(a, 0, len, 1);
return a[0];
}
// BEGIN CUT HERE
public:
void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); }
private:
template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); }
void verify_case(int Case, const int &Expected, const int &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } }
void test_case_0() { int Arg0 = 3; int Arg1 = 7; int Arg2 = 6; verify_case(0, Arg2, count(Arg0, Arg1)); }
void test_case_1() { int Arg0 = 4; int Arg1 = 13; int Arg2 = 120; verify_case(1, Arg2, count(Arg0, Arg1)); }
void test_case_2() { int Arg0 = 10; int Arg1 = 100; int Arg2 = 294844622; verify_case(2, Arg2, count(Arg0, Arg1)); }
void test_case_3() { int Arg0 = 123456789; int Arg1 = 12345; int Arg2 = 235511047; verify_case(3, Arg2, count(Arg0, Arg1)); }
// END CUT HERE
};
// BEGIN CUT HERE
int main() {
Nim ___test;
___test.run_test(-1);
return 0;
}
// END CUT HERE
【SRM】518 Nim的更多相关文章
- 【CF662A】Gambling Nim 线性基
[CF662A]Gambling Nim 题意:n长卡牌,第i张卡牌正面的数字是$a_i$,反面的数字是$b_i$,每张卡牌等概率为正面朝上或反面朝上.现在Alice和Bob要用每张卡牌朝上的数字玩N ...
- 【BZOJ3105】新Nim游戏(线性基)
[BZOJ3105]新Nim游戏(线性基) 题面 BZOJ Description 传统的Nim游戏是这样的:有一些火柴堆,每堆都有若干根火柴(不同堆的火柴数量可以不同).两个游戏者轮流操作,每次可以 ...
- 【LeetCode】518. Coin Change 2 解题报告(Python)
[LeetCode]518. Coin Change 2 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目 ...
- 【Leetcode】292. Nim Game
problem 292. Nim Game solution class Solution { public: bool canWinNim(int n) { ; } }; 来generalize一下 ...
- 【Leetcode】292. Nim游戏
题目链接:https://leetcode-cn.com/problems/nim-game/description/ 您和您的朋友,两个人一起玩 Nim游戏:桌子上有一堆石头,每次你们轮流拿掉 1 ...
- 【BZOJ3105】【CQOI2013】新Nim游戏
Description 传统的Nim游戏是这样的:有一些火柴堆,每堆都有若干根火柴(不同堆的火柴数量可以不同).两个游戏者轮流操作,每次可以选一个火柴堆拿走若干根火柴.可以只拿一根,也可以拿走整堆火柴 ...
- 【bzoj3105】新Nim游戏
Portal--> bzoj3105 新Nim游戏 Solution 转化一下问题 首先看一下原来的Nim游戏,先手必胜的条件是:每堆数量的异或和不为\(0\) 所以在新的游戏中,如果要保证自己 ...
- 【bzoj4589】Hard Nim FWT
题目描述 Claris和NanoApe在玩石子游戏,他们有n堆石子,规则如下: 1. Claris和NanoApe两个人轮流拿石子,Claris先拿. 2. 每次只能从一堆中取若干个,可将一堆全取走, ...
- 【LeetCode】292. Nim Game 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
随机推荐
- windows7 gvim 配置(好用)
http://blog.csdn.net/anders_zhuo/article/details/8949003
- instanceof操作符
instanceof是Java.php的一个二元操作符(运算符),和==,>,<是同一类东西.由于它是由字母组成的,所以也是Java的保留关键字.它的作用是判断其左边对象是否为其右边类的实 ...
- eclipse中ctrl+h默认打开是JavaSearch,怎么设置成默认打开是FileSearch
window->preferences->General->keys. 找到File Search(有搜索框的,可以搜索),然后在下方 Binding按下ctrl +h .
- OpenCV中IplImage图像格式与BYTE图像数据的转换
最近在将Karlsruhe Institute of Technology的Andreas Geiger发表在ACCV2010上的Efficent Large-Scale Stereo Matchin ...
- Memcache的增删改查
Memcache是把数据存放到内存的一种缓存技术,为了提高访问的速度,memcache存储的数据一般是频繁.不太重要的数据,php使用memcache,需要两步: (1).php_memcache.d ...
- Linq学习笔记四之linq to sql 的基本操作
首先需要在项目中新增一个 linq to sql 的服务 新增项,数据,LINQ TO sql 类的这个方法 第二步需要一个model类,用作映射 [Table] public class S_ZD ...
- java中面向对象的一些知识(一)
一:基本概念 举例:写一个程序,实现如下功能: 一群宠物,宠物有各种类型,如猫.狗.企鹅等 让这群宠物,按照各自的能力不同,进行各种比赛(如爬树.游泳.跳水):扩展性需求: 游泳比赛游泳池的参数.飞盘 ...
- Linux安装mariadb二进制版本
上一篇说了mariadb编译安装过程,但在生产环境中一般使用发布好的二进制版本,由于安装过程和之前一样,不再详细叙述,只是简单概括一下安装过程: 1. 下载 地址为:https://downloads ...
- xtrabackup 使用说明(续)
背景: 关于物理备份工具xtrabackup的一些说明可以先看之前写过的文章说明:xtrabackup 安装使用.现在xtrabackup版本升级到了2.4.4,相比之前的2.1有了比较大的变化:in ...
- OnlineTV 电视播放工具
通过网络使用电脑收看电视节目的播放工具,跟其他播放软件不同的是增加了录制功能. getList_bin_src.7z 获取电视直播源 OnlineTV-20161005.tar.xz OnlineTV ...