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
  • 相关阅读:
    两种类型插件系统
    ArcEngine序列化IFeatureRenderer对象
    [WorldWind学习]14.ConfigurationLoader类
    [WorldWind学习]15.模型加载
    AE应用程序开发的思考
    [C++摘抄]void*
    XML文件读写学习
    EA正向工程
    Word2010利用公式编辑器实现公式“显示”“居中”公式编号右对齐
    [Qt]Hello Qt !
  • 原文地址:https://www.cnblogs.com/JDFZ-ZZ/p/13562368.html
Copyright © 2011-2022 走看看