ACM-ICPC 2018 南京赛区网络预赛 K. The Great Nim Game

题意:有N堆石子(N为大数),每堆的个数按一定方式生成,问先手取若干堆进行尼姆博弈,必胜的方式有多少种。

题解:因为 k < 4096,所以出现的数最多只有4096个,对每个数字只考虑出现奇/偶次进行dp,答案是所有不等于0的dp值的和。然后如果一个数字出现x次,它对自己出现奇数次的方案数和偶数次的方案数贡献都是乘上2 ^ (x - 1),每个数字的贡献都是2 ^ (x - 1) 总贡献就是2 ^ (N - ∑(vis[i]))。大数可以一边读入一边取模。

  1 #include <bits/stdc++.h>
  2 using namespace std;
  3 #define ll long long
  4 #define ull unsigned long long
  5 #define mst(a,b) memset((a),(b),sizeof(a))
  6 #define mp(a,b) make_pair(a,b)
  7 #define fi first
  8 #define se second
  9 #define pi acos(-1)
 10 #define pii pair<int,int>
 11 const int INF = 0x3f3f3f3f;
 12 const double eps = 1e-6;
 13 const int MAXN = 1e7 + 10;
 14 const int MAXM = 2e6 + 10;
 15 const ll mod = 1e9 + 7;
 16
 17 bool vis[4105];
 18 int dp[2][8192];
 19 vector<int>vec;
 20 int f[4105];
 21
 22 ll pow_mod(ll a, ll n) {
 23     ll ans = 1;
 24     while(n) {
 25         if(n & 1) ans = ans * a % mod;
 26         a = a * a % mod;
 27         n >>= 1;
 28     }
 29     return ans;
 30 }
 31
 32 int main() {
 33 #ifdef local
 34     freopen("data.txt", "r", stdin);
 35 //    freopen("data.txt", "w", stdout);
 36 #endif
 37     ll ans = 0;
 38     char ch;
 39     int len = 0;
 40     int n = 1e9;
 41     while(1) {
 42         scanf("%c", &ch);
 43         if(ch == ‘ ‘) break;
 44         ans = pow_mod(ans, 10);
 45         if(len == 0) ans = 1;
 46         ans = ans * (1ll << (ch - ‘0‘)) % mod;
 47         if(len <= 5) {
 48             if(len == 0)
 49                 n = 0;
 50             n = n * 10 + ch - ‘0‘;
 51             len++;
 52         }
 53     }
 54     int x;
 55     scanf("%d", &x);
 56     int temp = x;
 57     int a, b, c, d, e, k;
 58     scanf("%d%d%d%d%d%d", &a, &b, &c, &d, &e, &k);
 59     vec.push_back(x);
 60     vis[x] = true;
 61     for(int i = 1; i <= 4100; i++) {
 62         int x1 = 1ll * a * i * i % k * i * i % k;
 63         int x2 = 1ll * b * i * i % k * i % k;
 64         int x3 = 1ll * c * i * i % k;
 65         int x4 = 1ll * d * i;
 66         x = (x1 + x2 + x3 + x4 + e - 1) % k + 1;
 67         f[i] = x;
 68     }
 69     int all = 0;
 70     while(all < n) {
 71         all++;
 72         int now = f[temp];
 73         if(!vis[now]) {
 74             vis[now] = true;
 75             vec.push_back(now);
 76             temp = now;
 77         } else break;
 78     }
 79     int sz = vec.size();
 80     dp[0][0] = 1;
 81     int pre = 1, now = 0;
 82     for(int i = 1; i <= sz; i++) {
 83         swap(pre, now);
 84         int num = vec[i - 1];
 85         for(int j = 0; j < 8192; j++) dp[now][j] = dp[pre][j];
 86         for(int j = 0; j < 8192; j++) {
 87             dp[now][j ^ num] += dp[pre][j];
 88             if(dp[now][j ^ num] >= mod) dp[now][j ^ num] -= mod;
 89         }
 90     }
 91     ll sum = 0;
 92     for(int i = 1; i < 8192; i++) {
 93         sum += dp[now][i];
 94         if(sum >= mod) sum -= mod;
 95     }
 96     ll inv = pow_mod(2, sz);
 97     inv = pow_mod(inv, mod - 2);
 98     ans = ans * inv % mod;
 99     ans = ans * sum % mod;
100     printf("%lld\n", ans);
101     return 0;
102 }

原文地址:https://www.cnblogs.com/scaulok/p/9573791.html

时间: 2024-08-30 05:13:22

ACM-ICPC 2018 南京赛区网络预赛 K. The Great Nim Game的相关文章

ACM-ICPC 2018 南京赛区网络预赛 J.Sum

Sum A square-free integer is an integer which is indivisible by any square number except 11. For example, 6 = 2 \cdot 36=2⋅3 is square-free, but 12 = 2^2 \cdot 312=22⋅3 is not, because 2^222 is a square number. Some integers could be decomposed into

ACM-ICPC 2018 南京赛区网络预赛 L. Magical Girl Haze

262144K There are NN cities in the country, and MM directional roads from uu to v(1\le u, v\le n)v(1≤u,v≤n). Every road has a distance c_ici?. Haze is a Magical Girl that lives in City 11, she can choose no more than KK roads and make their distances

计蒜客 ACM-ICPC 2018 南京赛区网络预赛 A. An Olympian Math Problem-数学公式题

A. An Olympian Math Problem 54.28% 1000ms 65536K Alice, a student of grade 66, is thinking about an Olympian Math problem, but she feels so despair that she cries. And her classmate, Bob, has no idea about the problem. Thus he wants you to help him.

ACM-ICPC 2018 南京赛区网络预赛 E题

ACM-ICPC 2018 南京赛区网络预赛 E题 题目链接: https://nanti.jisuanke.com/t/30994 Dlsj is competing in a contest with n (0 < n \le 20)n(0<n≤20) problems. And he knows the answer of all of these problems. However, he can submit ii-th problem if and only if he has s

ACM-ICPC 2018 南京赛区网络预赛 Lpl and Energy-saving Lamps 线段树

目录 ACM-ICPC 2018 南京赛区网络预赛 Lpl and Energy-saving Lamps 线段树 题面 题意 思路 ACM-ICPC 2018 南京赛区网络预赛 Lpl and Energy-saving Lamps 线段树 题面 During tea-drinking, princess, amongst other things, asked why has such a good-natured and cute Dragon imprisoned Lpl in the

ACM-ICPC 2018 焦作赛区网络预赛 K题 Transport Ship

There are NN different kinds of transport ships on the port. The i^{th}ith kind of ship can carry the weight of V[i]V[i] and the number of the i^{th}ith kind of ship is 2^{C[i]} - 12C[i]?1. How many different schemes there are if you want to use thes

ACM-ICPC 2018 沈阳赛区网络预赛 K. Supreme Number

A prime number (or a prime) is a natural number greater than 11 that cannot be formed by multiplying two smaller natural numbers. Now lets define a number NN as the supreme number if and only if each number made up of an non-empty subsequence of all

ACM-ICPC 2018 南京赛区网络预赛

A An Olympian Math Problem #include <bits/stdc++.h> using namespace std; typedef long long ll; ll T,n; int main(){ scanf("%lld",&T); while(T--){ scanf("%lld",&n); printf("%lld\n",n-1); } } B The writing on the w

线性素数筛 ACM-ICPC 2018 南京赛区网络预赛 J Sum

https://www.jisuanke.com/contest/1555?view=challenges 题意: 题解:写完都没发现是个积性函数233 想法就是对x分解质因数,f(x)就是2^k,其中k是x分解结果中次数为一的质因子个数.如果有某个次数大于等于3,f(x)==0; 这样明显会TLE 所以就想一个递推的方法. 于是魔改了一下线性筛. #include<iostream> #include<cstdlib> #include<cstdio> #includ