2017中国大学生程序设计竞赛 - 网络选拔赛 && HDU 6156 Palindrome Function 【数位DP】

Palindrome Function

Time Limit: 8000/4000 MS (Java/Others)    Memory Limit:256000/256000 K (Java/Others)
Total Submission(s): 713    Accepted Submission(s): 383

Problem Description

As we all know,a palindrome number is the number which reads the same backward as forward,such as 666 or 747.Some numbers are not the palindrome numbers in decimal form,but in other base,they may become the palindrome number.Like 288,it’s not a palindrome number under 10-base.But if we convert it to 17-base number,it’s GG,which becomes a palindrome number.So we define an interesting function f(n,k) as follow:
f(n,k)=k if n is a palindrome number under k-base.
Otherwise f(n,k)=1.
Now given you 4 integers L,R,l,r,you need to caluclate the mathematics expression 
Ri=Lrj=lf(i,j) .
When representing the k-base(k>10) number,we need to use A to represent 10,Bto represent 11,C to repesent 12 and so on.The biggest number is Z(35),so we only discuss about the situation at most 36-base number.

 

 

Input

The first line consists of an integer T,which denotes the number of test cases.
In the following T lines,each line consists of 4 integers L,R,l,r.
(1≤
T≤105,1≤LR≤109,2≤lr≤36)

 

 

Output

For each test case, output the answer in the form of “Case #i: ans” in a seperate line.

 

 

Sample Input

3

1 1 2 36

1 982180 10 10

496690841 524639270 5 20

 

 

Sample Output

Case #1: 665

Case #2: 1000000

Case #3: 447525746

 


【题意】

定义f(n,k),当n在k进制下为回文数时,f(n,k)为k,否则为1。

【思路】


用dp[i][j][k][1]表示k进制下长度为i枚举到了第j位时的回文数数量。


用dp[i][j][k][0]表示k进制下长度为i枚举到了第j位时的非回文数数量


然后就按照数位DP的固定格式写就好了。


【PS】这题用G++提交能节省一半的时间,C++提交易超时!!!



#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define mst(a,b) memset((a),(b),sizeof(a))
#define rush() int T;scanf("%d",&T);while(T--)

typedef long long ll;
const int maxn = 35;
const ll mod = 1e9+7;
const int INF = 0x3f3f3f;
const double eps = 1e-9;

ll bit[maxn];
ll temp[maxn];
ll dp[maxn][maxn][40][2];  //dp[i][j][k][1]表示k进制下长度为i枚举到了第j位时的回文数数量

ll dfs(ll len,ll cur,ll base,ll limit,ll st)
{
    if(cur<0) return st;
    if(limit==0&&~dp[len][cur][base][st])
        return dp[len][cur][base][st];
    int up=limit?bit[cur]:base-1;
    ll ans=0;
    for(int i=0;i<=up;i++)
    {
        temp[cur]=i;
        if(len==cur&&i==0)
        {
            ans=ans+dfs(len-1,cur-1,base,limit&&i==up,st);
        }
        else if(cur<(len+1)/2&&st)
        {
            ans=ans+dfs(len,cur-1,base,limit&&i==up,temp[len-cur]==i);
        }
        else
        {
            ans=ans+dfs(len,cur-1,base,limit&&i==up,st);
        }
    }
    return limit?ans:dp[len][cur][base][st]=ans;
}

ll solve(ll x,ll base)
{
    int len=0;
    while(x)
    {
        bit[len++]=x%base;
        x/=base;
    }
    mst(temp,-1);
    return dfs(len-1,len-1,base,1,1);
}

int main()
{
    ll L,R,l,r;
    mst(dp,-1);
    int cas=1;
    rush()
    {
        scanf("%I64d%I64d%I64d%I64d",&L,&R,&l,&r);
        ll ans=0;
        for(int i=l;i<=r;i++)
        {
            ll cnt=solve(R,i)-solve(L-1,i);    //k进制时区间内回文数个数
            ans+=cnt*i+(R-L+1-cnt);
        }
        printf("Case #%d: %I64d\n",cas++,ans);
    }
    return 0;
}









  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
资源包主要包含以下内容: ASP项目源码:每个资源包中都包含完整的ASP项目源码,这些源码采用了经典的ASP技术开发,结构清晰、注释详细,帮助用户轻松理解整个项目的逻辑和实现方式。通过这些源码,用户可以学习到ASP的基本语法、服务器端脚本编写方法、数据库操作、用户权限管理等关键技术。 数据库设计文件:为了方便用户更好地理解系统的后台逻辑,每个项目中都附带了完整的数据库设计文件。这些文件通常包括数据库结构图、数据表设计文档,以及示例数据SQL脚本。用户可以通过这些文件快速搭建项目所需的数据库环境,并了解各个数据表之间的关系和作用。 详细的开发文档:每个资源包都附有详细的开发文档,文档内容包括项目背景介绍、功能模块说明、系统流程图、用户界面设计以及关键代码解析等。这些文档为用户提供了深入的学习材料,使得即便是从零开始的开发者也能逐步掌握项目开发的全过程。 项目演示与使用指南:为帮助用户更好地理解和使用这些ASP项目,每个资源包中都包含项目的演示文件和使用指南。演示文件通常以视频或图文形式展示项目的主要功能和操作流程,使用指南则详细说明了如何配置开发环境、部署项目以及常见问题的解决方法。 毕业设计参考:对于正在准备毕业设计的学来说,这些资源包是绝佳的参考材料。每个项目不仅功能完善、结构清晰,还符合常见的毕业设计要求和标准。通过这些项目,学可以学习到如何从零开始构建一个完整的Web系统,并积累丰富的项目经验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值