ACM-ICPC 2018 南京賽區網路預賽__K The Great Nim Game【博弈論+費馬小定理+DP】
- 1500ms
- 131072K
Nim is a famous game as you know. Nim is a 2-player game featuring several piles of stones. Players alternate turns, and on his/her turn, a player's move consists of removing one or more stones from any single pile. Play ends when all the stones have been removed. The first player who can't remove is declared as the loser.
Now you want to play the Great Nim Game. In the other words, you want to choose several (0 ~ N) pile(s) from N piles of stones. You want know how many choices you have making sure that the first player must win. They both try their best (optimal strategy) to win through the game.
Input
The first line contains two numbers N x1, denoting the number of piles and the number of stones in the first pile.
The second line contains five integers a, b, c, d, e
The third line contains one integer kk, denoting a function
f(x)=
(ax^4+bx^3+cx^2+dx^1+e - 1)%k+1.
With these, you can figure out the number of stones in the i-th pile xi=f(xi−1)(1<i≤N)
It is guaranteed that
1<N<1010000000,
0<x1≤k,0≤a,b,c,d,e<2^12,
a+b+c+d+e>0,0<k<2^12.
Output
Print the number of solutions making sure the first player must win. The answer may be very large, so you should output it mod 1e9+7 (% 1000000007).
Hint
In the first sample, there are 1,2,3,4,5 stones in the 1-st, 2-nd, 3th, 4th, 5th pile. You can figure out there are exactly 4 choosing ({1,2,3}{1,4,5}{2,3,4,5}{}(empty, you choose zero pile)) ways that make first-hand player must lose, so the answer is 2^5-4=2825−4=28.
If x is in range (1<=x<= k), f(x)(1≤x≤k),f(x) must be in range (1≤f(x)≤k), too.
樣例輸入1
5 1
0 0 0 1 1
16
樣例輸出1
28
樣例輸入2
100000000000000000000 1
0 0 0 1 1
4095
樣例輸出2
394326889
樣例輸入3
100000000000000000000 1
1 0 0 1 1
4095
樣例輸出3
933180537
題目來源
參考部落格:https://blog.csdn.net/nudt_spy/article/details/82453579
AC的C++程式碼:
#include<iostream>
#include<string>
#include<cstring>
using namespace std;
typedef long long ll;
const int mod=1e9+7;
const int N=4100;
bool flag[N];//vis[i]記錄數i是否出現
int X[N];//記錄X[i]的值
int dp[2][N];
ll f(ll x,ll n)
{
ll res=1;
while(n>0){
if(n&1)
res=res*x%mod;
x=x*x%mod;
n>>=1;
}
return res;
}
int main()
{
string s;
ll a,b,c,d,e,k,x;
while(cin>>s>>x>>a>>b>>c>>d>>e>>k){
ll t=0;
int num=0;//num記錄能出現多少個不同的x
for(int i=0;i<s.length();i++){
t=(t*10+(s[i]-'0'))%(mod-1);
if(num<=4096)
num=num*10+(s[i]-'0');
}
memset(flag,false,sizeof(flag));
memset(dp,0,sizeof(dp));
X[1]=x;//X1為x
flag[x]=true;//x這個數出現了
for(int i=2;i<=num;i++){//計算這num個不同的x值
int f=X[i-1];
X[i]=(a*f*f*f*f+b*f*f*f+c*f*f+d*f+e-1)%k+1;
//如果X[i]這個數已經出現了那麼後面的x都是已經出現的數了,這時就跳出
if(flag[X[i]]){
num=i-1;
break;
}
else//如果沒出現就標記
flag[X[i]]=true;
}
dp[0][0]=1;
int x=0,y=1;
for(int i=1;i<=num;i++,swap(x,y))//考慮這num個不同的數
for(int j=0;j<N;j++)
dp[y][j]=(dp[x][j^X[i]]+dp[x][j])%mod;
ll ans=0;
for(int j=1;j<4096;j++)
ans=(ans+dp[x][j])%mod;
t=(t-num%(mod-1)+(mod-1))%(mod-1);
cout<<ans*f(2,t)%mod<<endl;
}
return 0;
}
相關文章
- ACM-ICPC 2018 南京賽區網路預賽__E AC Challenge【狀態壓縮+DP】ACM
- ACM-ICPC 2018 南京賽區網路預賽__B The writing on the wall【列舉】ACM
- ACM-ICPC 2018 徐州賽區網路預賽ACM
- ACM-ICPC 2018 瀋陽賽區網路預賽ACM
- ACM-ICPC 2018 徐州賽區網路預賽 F. Features TrackACM
- ACM-ICPC 2018 南京賽區網路預賽 __G Lpl and Energy-saving Lamps【線段樹+模擬】ACMLAMP
- ACM-ICPC 2018 南京賽區網路預賽__J. Sum【尤拉篩法+質因子分解+思維】ACM
- ACM-ICPC 2018 南京賽區網路預賽__L. Magical Girl Haze 【Dijkstra演算法+分層圖思想】ACM演算法
- ACM-ICPC 2018 徐州賽區網路預賽 I. Characters with Hash【簽到題】ACM
- 費馬小定理-期望dp
- 2018icpc 南京網路賽L Magical Girl Haze
- 2018 瀋陽賽區網路預賽 I.Lattice's basics in digital electronics(模擬)Git
- 2019ICPC南京網路賽B super_log——擴充套件尤拉定理套件
- 博弈論:公平組合遊戲(Nim 遊戲 & SG 定理)學習筆記遊戲筆記
- 2018 北京賽區網路預選賽 A. Saving Tang Monk II(BFS+優先佇列)佇列
- 田忌賽馬博弈矩陣分析矩陣
- 2018 ICPC南京區域賽題解 更新至 8 題
- 同構——費馬小定理
- 【博弈論】HDU - 7216 Triangle GameGAM
- HDU 5794 A Simple Chess (lucas定理+費馬小定理)
- 博弈論基礎之sg函式與nim函式
- 2018 徐州網路賽 G 題解
- 第43屆ACM-ICPC國際大學生程式設計競賽 亞洲區域賽南京站現場賽名額分配相關說明ACM程式設計
- 費馬定理
- 2018華為網路技術大賽
- 費馬小定理 + 費馬大定理 + 勾股數的求解 + 快速冪 + 矩陣快速冪 【模板】矩陣
- zoj-4053(2018ICPC青島網路賽K題)啟發式分裂
- Cognitive Flow: The Psychology of Great Game DesigGAM
- Leetcode 292. Nim GameLeetCodeGAM
- 博弈論小記
- 為加快南京AI產業發展,2018全球(南京)人工智慧應用大賽現公開徵集賽題原型AI產業人工智慧原型
- 沙漠or綠洲,從南京軟博會I.D.Spark網際網路創新大賽看南京網際網路發展Spark
- 2020ICPC·小米 網路選拔賽熱身賽K-Random Point in Trianglerandom
- E - Remove Pairs(狀壓dp+博弈論)REMAI
- 2024 ICPC 網路預選賽 第 2 場
- 西湖論劍·2022中國杭州網路安全技能大賽線上初賽火熱開賽
- 數論入門基礎(同餘定理/費馬小定理/擴充套件歐幾里德演算法/中國剩餘定理)套件演算法
- 影片區域性打馬賽克