Codeforces Manthan Codefest 17 E. Salazar Slytherin‘s Locket 题解 数位dp

Salazar Slytherin’s Locket

题目描述

Harry came to know from Dumbledore that Salazar Slytherin’s locket is a horcrux. This locket was present earlier at 12 Grimmauld Place, the home of Sirius Black’s mother. It was stolen from there and is now present in the Ministry of Magic in the office of Dolorous Umbridge, Harry’s former Defense Against the Dark Arts teacher.

Harry, Ron and Hermione are infiltrating the Ministry. Upon reaching Umbridge’s office, they observed a code lock with a puzzle asking them to calculate count of magic numbers between two integers l l l and r r r (both inclusive).

Harry remembered from his detention time with Umbridge that she defined a magic number as a number which when converted to a given base b b b , all the digits from 0 0 0 to b − 1 b-1 b1 appear even number of times in its representation without any leading zeros.

You have to answer q q q queries to unlock the office. Each query has three integers b i b_{i} bi , l i l_{i} li and r i r_{i} ri , the base and the range for which you have to find the count of magic numbers.

输入格式

First line of input contains q q q ( 1 < = q < = 1 0 5 1<=q<=10^{5} 1<=q<=105 ) — number of queries.

Each of the next q q q lines contain three space separated integers b i b_{i} bi , l i l_{i} li , r i r_{i} ri ( 2 < = b i < = 10 2<=b_{i}<=10 2<=bi<=10 , 1 < = l i < = r i < = 1 0 18 1<=l_{i}<=r_{i}<=10^{18} 1<=li<=ri<=1018 ).

输出格式

You have to output q q q lines, each containing a single integer, the answer to the corresponding query.

题面翻译

题目

l . . . r l...r l...r 之间的所有数中,转成 b b b 进制后 0 , 1 , 2 , . . . , b − 2 , b − 1 0,1,2,...,b-2,b-1 0,1,2,...,b2,b1 均出现偶数次的数的个数。

输入

第一行一个数 q q q,为数据组数。

下面 q q q 行,每行 3 3 3 个整数,表示 b , l , r b,l,r b,l,r

输出

q q q 行,为每个询问的答案。

样例 #1

样例输入 #1

2
2 4 9
3 1 10

样例输出 #1

1
2

样例 #2

样例输入 #2

2
2 1 100
5 1 100

样例输出 #2

21
4

数据规模与约定

1 ≤ q ≤ 1 0 5 1\le q \le 10^5 1q105

2 ≤ b ≤ 10 2\le b \le 10 2b10

1 ≤ l ≤ r ≤ 1 0 18 1\le l \le r \le 10^{18} 1lr1018

原题

CF——传送门
洛谷——传送门

代码

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

ll dp[66][3000][16];                                      // dp记录[pos][mask][b]状态下对答案的贡献
int a[66];                                                // a[]记录b进制串
ll dfs(int pos, int mask, int b, bool bound, bool lead_0) // pos为此时的位置,mask表示0~b-1的出现次数的状态压缩,b表示进制数,bound表示前面每一位是否都是上界,lead_0表示是否前面全是0
{
    if (pos == 0) // 枚举完每一位时返回
    {
        if (lead_0 == 1)
            return 1; // 因为不统计数0,所以返回1还是返回0不影响结果
        else
        {
            if (mask == 0) // 如果最后所有0~b-1所有数都出现偶数(即全为0),则是符合条件的数字
                return 1;
            else
                return 0;
        }
    }
    if (!bound && !lead_0 && dp[pos][mask][b] != -1) // 读取记忆(doge
        return dp[pos][mask][b];
    int max_num; // 可枚举的该位的数的上界
    if (bound)
        max_num = a[pos];
    else
        max_num = b - 1;
    ll res = 0;
    for (int i = 0; i <= max_num; i++)
    {
        if (lead_0 && i == 0)
            res += dfs(pos - 1, 0, b, bound && (i == a[pos]), 1);
        else
            res += dfs(pos - 1, mask ^ (1 << i), b, bound && (i == a[pos]), 0); // mask^(1<<i)是计算出现次数的核心,0表示数i出现偶数,1表示数i出现奇数
    }
    if (!bound && !lead_0) // 没在边界时,记录下该状态对应的答案,记忆化
        dp[pos][mask][b] = res;
    return res;
}
ll solve(ll x, int b)
{
    int len = 0;
    while (x)
    {
        a[++len] = x % b;
        x /= b;
    }
    return dfs(len, 0, b, 1, 1);
}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    memset(dp, -1, sizeof(dp)); // 将dp数组初始化为-1,表示对应状态的答案目前还未计算出
    int q;
    cin >> q;
    int b;
    ll l, r;
    while (q--)
    {
        cin >> b >> l >> r;
        cout << solve(r, b) - solve(l - 1, b) << '\n';
    }

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值