zoukankan      html  css  js  c++  java
  • CF855E Salazar Slytherin's Locket

    Description

    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 ll and rr (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$ , all the digits from$0$ to $b-1$ appear even number of times in its representation without any leading zeros.

    You have to answer $q$ queries to unlock the office. Each query has three integers $b_i$ , $l_i$ and $r_i$ , the base and the range for which you have to find the count of magic numbers.

    求$l cdots r$之间转成$b$进制后,$0,1,2 cdots b-2,b-1$都出现偶数次的数的个数。

    Solution

    数位DP,有一个记忆化的优化

    如果从高位到低位枚举,可以发现对于确定的进制数,确定的剩余位数和确定的需要每个数字的奇偶状态,且不在边界,数的个数是一样的

    #include<iostream>
    #include<cstring>
    #include<cstdio>
    using namespace std;
    long long q,b,l,r,num[100],tot,dp[12][65][(1<<11)-1];
    inline long long read()
    {
        long long w=0,f=1;
        char ch=0;
        while(ch<'0'||ch>'9')
        {
            if(ch=='-')
                f=-1;
            ch=getchar();
        }
        while(ch>='0'&&ch<='9')
        {
            w=(w<<1)+(w<<3)+ch-'0';
            ch=getchar();
        }
        return w*f;
    }
    long long dfs(long long pos,long long sta,bool zer,bool lim)
    {
        if(!pos)
        {
            return !sta;
        }
        if(!zer&&!lim&&dp[b][pos][sta]!=-1)
        {
            return dp[b][pos][sta];
        }
        long long top=lim?num[pos]:b-1,ret=0;
        for(long long i=0;i<=top;i++)
        {
            ret+=dfs(pos-1,(!i&&zer)?sta:sta^(1<<i),!i&&zer,lim&&(i==top));
        }
        if(!zer&&!lim)
        {
            dp[b][pos][sta]=ret;
        }
        return ret;
    }
    long long solve(long long x)
    {
        tot=0;
        while(x)
        {
            num[++tot]=x%b;
            x/=b;
        }
        return dfs(tot,0,true,true);
    }
    int main()
    {
        memset(dp,-1,sizeof(dp));
        q=read();
        for(long long i=1;i<=q;i++)
        {
            b=read();
            l=read();
            r=read();
            printf("%lld
    ",solve(r)-solve(l-1));
        }
        return 0;
    }
    Salazar Slytherin's Locket
  • 相关阅读:
    750. Number Of Corner Rectangles
    [Project Euler] 3. Largest Prime factor
    [Project Euler] 2. Even Fibonacci numbers
    Jmeter学习笔记3-参数化
    SQL多表连接查询补充
    Jmeter学习笔记2-原件作用域与执行顺序
    Jmeter学习笔记1-实践介绍
    运用badboy录制jmeter脚本
    【SQL提数】左连接使用
    【功能测试技巧2】dubbo引起的数据精度的思考
  • 原文地址:https://www.cnblogs.com/JDFZ-ZZ/p/13562368.html
Copyright © 2011-2022 走看看