题意

\(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的更多相关文章

  1. 【CF662A】Gambling Nim 线性基

    [CF662A]Gambling Nim 题意:n长卡牌,第i张卡牌正面的数字是$a_i$,反面的数字是$b_i$,每张卡牌等概率为正面朝上或反面朝上.现在Alice和Bob要用每张卡牌朝上的数字玩N ...

  2. 【BZOJ3105】新Nim游戏(线性基)

    [BZOJ3105]新Nim游戏(线性基) 题面 BZOJ Description 传统的Nim游戏是这样的:有一些火柴堆,每堆都有若干根火柴(不同堆的火柴数量可以不同).两个游戏者轮流操作,每次可以 ...

  3. 【LeetCode】518. Coin Change 2 解题报告(Python)

    [LeetCode]518. Coin Change 2 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目 ...

  4. 【Leetcode】292. Nim Game

    problem 292. Nim Game solution class Solution { public: bool canWinNim(int n) { ; } }; 来generalize一下 ...

  5. 【Leetcode】292. Nim游戏

    题目链接:https://leetcode-cn.com/problems/nim-game/description/ 您和您的朋友,两个人一起玩 Nim游戏:桌子上有一堆石头,每次你们轮流拿掉 1  ...

  6. 【BZOJ3105】【CQOI2013】新Nim游戏

    Description 传统的Nim游戏是这样的:有一些火柴堆,每堆都有若干根火柴(不同堆的火柴数量可以不同).两个游戏者轮流操作,每次可以选一个火柴堆拿走若干根火柴.可以只拿一根,也可以拿走整堆火柴 ...

  7. 【bzoj3105】新Nim游戏

    Portal--> bzoj3105 新Nim游戏 Solution 转化一下问题 首先看一下原来的Nim游戏,先手必胜的条件是:每堆数量的异或和不为\(0\) 所以在新的游戏中,如果要保证自己 ...

  8. 【bzoj4589】Hard Nim FWT

    题目描述 Claris和NanoApe在玩石子游戏,他们有n堆石子,规则如下: 1. Claris和NanoApe两个人轮流拿石子,Claris先拿. 2. 每次只能从一堆中取若干个,可将一堆全取走, ...

  9. 【LeetCode】292. Nim Game 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

随机推荐

  1. Makefile的编写

    makefile介绍 makefile的功能是管理源文件的编译链接,在makefile我们可以定义一系列的规则来指定哪些文件需要先编译,哪些文件需要后编译,哪些文件需要重新编译,甚至于进行更复杂的功能 ...

  2. Python模块之"prettytable"

    Python模块之"prettytable" 摘要: Python通过prettytable模块可以将输出内容如表格方式整齐的输出.(对于用Python操作数据库会经常用到) 1. ...

  3. cron 任务

    相关文件 /etc/crontab /etc/cron.deny 设置哪个用户有权限运行 cron 任务 /var/spool/cron/root /var/spool/cron/user /var/ ...

  4. Oracle10g 表分区

    1.分区的原因 (1)Tables greater than 2GB should always be considered for partitioning. (2)Tables containin ...

  5. Android安全攻防战,反编译与混淆技术完全解析(下)

    在上一篇文章当中,我们学习了Android程序反编译方面的知识,包括反编译代码.反编译资源.以及重新打包等内容.通过这些内容我们也能看出来,其实我们的程序并没有那么的安全.可能资源被反编译影响还不是很 ...

  6. LeetCode 205 Isomorphic Strings

    Problem: Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if ...

  7. 关于计算机改名无法连接TFS的问题

    今天重新导入了两台服务器, 修改了机器名,结果VS2012链接TFS报错 --------------------------- Microsoft Visual Studio ----------- ...

  8. from表单如果未指定action,submit提交时候会执行当前url

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...

  9. Qt5.5.1编译出来的程序出现libgcc_s_dw2-1.dll的解决方案

    问题如图: 输入"myudp2016.exe 1  " 后出现 这是因为没有在系统环境变量path里加上相关路径,我们添加如下路径: 比如说WIN7系统-开始-计算机-右键-属性- ...

  10. Java笔记:修饰符

    Synchronized 修饰符 Synchronized 关键字声明的方法同一时间只能被一个线程访问.Synchronized 修饰符可以应用于四个访问修饰符. 实例 public synchron ...