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;
}
### 完美排列问题分析 对于 Codeforces Round 1007 (Div. 2) 中的 **B. Perfecto** 问题,目标是找到一个长度为 \( n \) 的完美排列。如果这样的排列存在,则输出该排列;否则输出 `-1`。 #### 题目解析 题目定义了一个“完美排列”,其条件如下: - 对于任意位置 \( i \),满足 \( |p_i - p_{i+1}| = 1 \) 或者 \( |p_i - p_{i+1}| = n-1 \)[^2]。 这意味着相邻两个元素之间的差值要么等于 1(即连续),要么等于 \( n-1 \)(即首尾相连)。 #### 解题方法 通过观察和归纳可以得出以下结论: - 当 \( n \% 3 == 0 \) 或 \( n \% 3 == 2 \) 时,无法构建出符合上述条件的完美排列。 - 而当 \( n \% 3 == 1 \) 时,可以通过特定构造方式生成所需排列。 具体实现逻辑如下: ```cpp #include <bits/stdc++.h> using namespace std; void solve() { int n; cin >> n; if (n % 3 != 1) { // 如果不符合模数条件 cout << "-1\n"; return; } vector<int> res(n); bool flag = true; // 控制交替模式 for(int i = 0;i < n;i++) { if(flag){ res[i] = i + 1; } else{ res[i] = ((n-i)+1)%n; if(res[i]==0)res[i]=n; } flag=!flag; } for(auto num : res){ cout<<num<<" "; } } int main(){ ios::sync_with_stdio(false); cin.tie(0); int t=1; while(t--){ solve(); } } ``` 此代码片段实现了基于输入大小 \( n \) 来判断是否存在合法解并输出相应结果的功能。 #### 关键点说明 - 判断依据来源于对不同余数值下能否形成循环结构的研究成果。 - 构造过程中采用交替填充策略来确保最终序列能够满足绝对差的要求。 --- ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值