【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 ...
随机推荐
- Maven的POM.xml配置大全
<?xml version="1.0" encoding="utf-8"?> <project xmlns="http://mave ...
- Angular2 组件通信
1. 组件通信 我们知道Angular2应用程序实际上是有很多父子组价组成的组件树,因此,了解组件之间如何通信,特别是父子组件之间,对编写Angular2应用程序具有十分重要的意义,通常来讲,组件之间 ...
- VC++ GetModuleFileName()获取路径字符串中带波浪线~
GetModuleFileName()获取的字符串中带波浪线,不是完整的路径显示. 原因:获取的是短路径,进行了缩写 解决:还原长路径 TCHAR }; GetLongPathName( strTem ...
- Git 常用操作和问题解决
记录一下自己用git作为项目管理过程中常见的错误以及处理方法 1.git pull 出现问题 git pull出现的问题多为远程分支文件和本地冲突 错误提示:error: Your local cha ...
- 第二天--html+css
<!Doctype html><html> <head> <meta charset="utf-8"> ...
- Werkzeug工具包学习-官方例子Shortly分析
为了学习werkzeug的wsgi框架工具,今天真对官网的例子进行调试运行.涉及到了werkzeug工具包,jinja2前端模版,以及redis内存库,之后可以灵活定制自己主页.再次,作以记录. 首先 ...
- html5 Application Cache 机制以及使用
那什么是Application Cache呢? 顾名思义,AppCache就是对app内存缓存的方案,具体表现为当请求某个文件时不是从网络获取该文件,而是从本地内存中获取. Application C ...
- Guidance of Set up FTP Server
Step 1. Create a FTP folder in your C disk, named "FTPReport"(an example) Step 2. Install ...
- shell脚本比较两个数大小
#/bin/bash read -p "请输入第一个数:" a read -p "请输入第二个数:" b if [ $a -gt $b ] #判断第一个数是不是 ...
- 【转】网络编程socket基本API详解
转自:http://www.cnblogs.com/luxiaoxun/archive/2012/10/16/2725760.html socket socket是在应用层和传输层之间的一个抽象层,它 ...