首页 > 其他分享 >F - Nim

F - Nim

时间:2024-01-27 17:45:38浏览次数:19  
标签:10 Nim int Sample leq Input oplus

F - Nim

Problem Statement

You are given integers $N,A_1,A_2,A_3$. Find the number, modulo $998244353$, of triples of positive integers $(X_1,X_2,X_3)$ that satisfy all of the following three conditions.

  • $1\leq X_i \leq N$ for every $i$.
  • $X_i$ is a multiple of $A_i$ for every $i$.
  • $(X_1 \oplus X_2) \oplus X_3 = 0$, where $\oplus$ denotes bitwise xor.
What is bitwise xor? The bitwise xor of non-negative integers $A$ and $B$, $A \oplus B$, is defined as follows.
  • When $A \oplus B$ is written in binary, the $2^k$s place ($k \geq 0$) is $1$ if exactly one of the $2^k$s places of $A$ and $B$ is $1$, and $0$ otherwise.
For instance, $3 \oplus 5 = 6$ (in binary: $011 \oplus 101 = 110$).

Constraints

  • $1 \leq N \leq 10^{18}$
  • $1 \leq A_i \leq 10$
  • All input values are integers.

Input

The Input is given from Standard Input in the following format:

$N$ $A_1$ $A_2$ $A_3$

Output

Print the answer.  


Sample Input 1

13 2 3 5

Sample Output 1

4

Four triples $(X_1,X_2,X_3)$ satisfy the conditions: $(6,3,5),(6,12,10),(12,6,10),(12,9,5)$.


Sample Input 2

1000000000000000000 1 1 1

Sample Output 2

426724011

Sample Input 3

31415926535897932 3 8 4

Sample Output 3

759934997

 

解题思路

  用刚学的递归写法来做这道数位 dp,请参考数位 dp 学习笔记(灵神模板)

  对 $X_1$,$X_2$ 和 $X_3$ 同时进行数位 dp,找的是满足条件的三元组的数量。很显然由于涉及到异或运算,需要把 $X_i$ 看作是二进制数。考虑参数的选择,由于要满足 $A_i \mid X_i$ 因此还需要用一个参数 $r_i$ 来记录前 $u$ 位所表示的二进制数模 $A_i$ 的值。另外由于询问的区间是 $[1, N]$,所以不可以有前导零。最后在枚举 $X_1$,$X_2$ 和 $X_3$ 第 $u$ 位选择的数 $x_1$,$x_2$,$x_3$ 时,要保证 $x_1 \oplus x_2 \oplus x_3 = 0$。

  一些技巧,事实上并不需要把 $X_i$ 转换成二进制字符串然后 dp,直接用 Xi >> u & 1 来得到 $X_i$ 在二进制下第 $u$ 位的值。由于 $N$ 最大为 $10^{18}$,因此二进制位最多有 $60$ 位,所以一开始传入的参数 $u$ 为 $u=59$,从高位往低位递归,边界条件是 $u = -1$。

  AC 代码如下,时间复杂度为 $O(\log{N} \cdot A^3)$:

#include <bits/stdc++.h>
using namespace std;

typedef long long LL;

const int N = 60, mod = 998244353;

LL n, a1, a2, a3;
int f[N][10][10][10][2][2][2][2][2][2];

int dfs(int u, int r1, int r2, int r3, int h1, int h2, int h3, int l1, int l2, int l3) {
    if (u < 0) return !r1 && !r2 && !r3 && !l1 && !l2 && !l3;
    if (f[u][r1][r2][r3][h1][h2][h3][l1][l2][l3] != -1) return f[u][r1][r2][r3][h1][h2][h3][l1][l2][l3];
    int ret = 0;
    int up1 = h1 ? n >> u & 1 : 1, up2 = h2 ? n >> u & 1 : 1, up3 = h3 ? n >> u & 1 : 1;
    for (int x1 = 0; x1 <= up1; x1++) {
        for (int x2 = 0; x2 <= up2; x2++) {
            for (int x3 = 0; x3 <= up3; x3++) {
                if (!(x1 ^ x2 ^ x3)) ret = (ret + dfs(u - 1, (r1 * 2 + x1) % a1, (r2 * 2 + x2) % a2, (r3 * 2 + x3) % a3, h1 && x1 == up1, h2 && x2 == up2, h3 && x3 == up3, l1 && !x1, l2 && !x2, l3 && !x3)) % mod;
            }
        }
    }
    return f[u][r1][r2][r3][h1][h2][h3][l1][l2][l3] = ret;
}

int main() {
    scanf("%lld %lld %lld %lld", &n, &a1, &a2, &a3);
    memset(f, -1, sizeof(f));
    printf("%d", dfs(59, 0, 0, 0, 1, 1, 1, 1, 1, 1));
    
    return 0;
}

 

参考资料

  Editorial - GAMEFREAK Programming Contest 2023 (AtCoder Beginner Contest 317):https://atcoder.jp/contests/abc317/editorial/7047

标签:10,Nim,int,Sample,leq,Input,oplus
From: https://www.cnblogs.com/onlyblues/p/17991695

相关文章

  • OpenIM Open Source Instant Messaging Project Docker Compose Deployment Guide
    ThedeploymentofOpenIMinvolvesmultiplecomponentsandsupportsvariousmethodsincludingsourcecode,Docker,andKubernetes.Thisrequiresensuringcompatibilitybetweendifferentdeploymentmethodsandeffectivelymanagingdifferencesbetweenversio......
  • Open Source Instant Messaging (IM) Project OpenIM Source Code
    DeployingOpenIMinvolvesmultiplecomponentsandsupportsvariousmethods,includingsourcecode,Docker,andKubernetes.Thisrequiresensuringcompatibilitybetweendifferentdeploymentmethodswhileeffectivelymanagingdifferencesbetweenversions.I......
  • OpenIM Open Source Instant Messaging Project Docker Compose Deployment Guide
    ThedeploymentofOpenIMinvolvesmultiplecomponentsandsupportsvariousmethodsincludingsourcecode,Docker,andKubernetes.Thisrequiresensuringcompatibilitybetweendifferentdeploymentmethodsandeffectivelymanagingdifferencesbetweenversio......
  • vue3中使用animate.css+wow.js
    官网链接:animatewow.js版本声明:"dependencies":{"vue":"^3.3.11","animate.css":"^4.1.1","wow.js":"^1.2.2"},1.安装:npminstallanimate.css--savenpminstallwow.js......
  • MiniMax 国内首个MoE大语言模型全量上线啦
    MiniMax国内首个MoE大语言模型全量上线啦今天,经过了半个月的部分客户的内测和反馈,MiniMax全量发布大语言模型abab6,为国内首个MoE大语言模型。在MoE结构下,abab6拥有大参数带来的处理复杂任务的能力,同时模型在单位时间内能够训练足够多的数据,计算效率也可以得到大幅提升。......
  • 在vue2中使用leaflet.AnimatedMarker来移动某个目标
    需求是:点击某个按钮后让扫描仪沿着某条线移动进行扫描效果:  扫描仪是沿着河流移动的,河流的生成方式通过geojson数据生成,geojson里包含了河流的一些点位的经纬度,扫描仪根据经纬度来移动leaflet:1.9.4 leaflet.AnimatedMarker:1.0.0 1.引入 importLfrom'leaf......
  • "nim-lang" 的 nim c -r 解析
    nimc-rhello.nim是一个在命令行中运行的命令,用于编译并运行Nim语言编写的程序。这个命令可以分解为以下几个部分:nim:这是Nim编译器的命令行工具。你需要先安装Nim,并确保它的路径已经添加到了环境变量中,才能在命令行中使用这个命令。c:这是一个命令行选项,代表"co......
  • Delphi Animation
     AnimateFloat是Delphi中用于创建简单动画效果的一个函数,它可以让你平滑地改变控件的属性值,例如位置、大小、透明度等。通过指定起始值和目标值,以及动画持续时间,AnimateFloat函数可以实现属性值的过渡动画效果。下面是AnimateFloat函数的语法:procedureAnimateFloat(co......
  • 博弈论 & Nim 游戏
    公平组合游戏ICG:1.有两名玩家参与2.在游戏的任意时刻,玩家执行的合法行动与轮到那名玩家无关3.不能行动的玩家判负Nim游戏:**给定n堆物品,第i堆物品有Ai个,两名玩家轮流行动,可以取走每堆任意多个(>0),取走最后一件物品的玩家获胜,这种游戏称为NIM游戏,**定理:NIM先手必......
  • Nuxt3教程:添加Autoanimate 动画库
    前言AutoAnimate是一个零配置,插入式动画实用程序,可以为您的Web应用程序添加平滑过渡。您可以将其与React,Solid,Vue,Svelte或任何其他JavaScript应用程序一起使用。正文安装依赖#yarnyarnadd@formkit/auto-animate#npmnpminstall@formkit/auto-animate#pnpmpnpmadd......