lightoj1205(数位DP)

地址:http://www.lightoj.com/volume_showproblem.php?problem=1205

1205 - Palindromic Numbers
Time Limit: 2 second(s)Memory Limit: 32 MB

A palindromic number or numeral palindrome is a 'symmetrical' number like 16461 that remains the same when its digits are reversed. In this problem you will be given two integers i j, you have to find the number of palindromic numbers between i and j (inclusive).

Input

Input starts with an integer T (≤ 200), denoting the number of test cases.

Each case starts with a line containing two integers i j (0 ≤ i, j ≤ 1017).

Output

For each case, print the case number and the total number of palindromic numbers between i and (inclusive).

Sample Input

Output for Sample Input

4

1 10

100 1

1 1000

1 10000

Case 1: 9

Case 2: 18

Case 3: 108

Case 4: 198

 

题意:求区间[a,b]的回文数个数。

思路:通过确定第一位数字来确定最后一位数字,这样寻找一个数位为8的回文数可以通过确定第一位来寻找数位为6的回文数,以此类推。需要注意的是当首个数字为0是寻找的是有该数位数减1的数位的回文数,还有就是有些数字其第一位最大值在搜索时是达不到的,因为我们同时确定两位数,有可能另一位数达不到该位。做了一些数位DP的题,发现对于首位为0会有不同结果的题目来说,最好在自己要开的数组里多加一维来处理是否为前导0的情况。

代码:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
using namespace std;
#define LL long long
LL dp[25][2];  //多开一维处理是否为前导0的情况
int num[25],len1;
LL dfs(int len,int wz,bool p,bool z)  //wz是确定目前考虑数字位置,z是确定是否有前导1
{
    if(!len||len==-1) return 1;
    if(!p&&~dp[len][z]) return dp[len][z];
    LL ans=0;
    if(!p)  //对于不置顶的搜索可以直接进行
        for(int i=0;i<=9;i++)
            ans+=dfs(z|i?len-2:len-1,wz-1,0,z|i);
    else  //置顶的搜索可能会遇到例如1000的情况,即第1位可以确定为1,但最后一位不可以
    {
        int u=num[wz];
        for(int i=0;i<u;i++)
            ans+=dfs(z|i?len-2:len-1,wz-1,p&i==u,z|i);
        if(num[wz]>num[len1-wz+1])  //如果本位以中心为轴对应的位置不能与本位同时达到极值
        {
            int j=2;
            while(!num[len1-wz+j]&&len1-wz+j<=len1) j++;  //寻找在本位以中心为轴对应的位置与本位之间不为0的位置
            if(len1-wz+j>=wz) return ans;  //找不到就直接返回ans
            else
            {
                num[len1-wz+j]--;  //该位置减一表示我从前面取一位
                for(int i=len1-wz+j-1;i>=len1-wz+1;i--)
                    num[i]=9;  //其余全置为9
            }
        }
        ans+=dfs(z|u?len-2:len-1,wz-1,1,z|u);  //找到就加上这一组搜索
    }
    if(!p) dp[len][z]=ans;
    return ans;
}
LL getans(LL m)
{
    if(m<0) return 0;
    LL ans=0;len1=0;
    for(;m;m/=10)
        num[++len1]=m%10;
    return dfs(len1,len1,1,0);
}
int main()
{
    memset(dp,-1,sizeof(dp));
    LL l,r;
    int t,cas=1;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%lld%lld",&l,&r);
        if(l>r) {l^=r;r^=l;l^=r;}  //注意本题可以小数在后
        printf("Case %d: %lld\n",cas++,getans(r)-getans(l-1));
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Sigma函数是指一个数字的所有因子之和。给定一个数字n,需要求出有多少个数字的Sigma函数是偶数。\[2\] 为了解决这个问题,可以先筛选出n范围内的素数(范围在10^6即可),然后对n进行素因子分解。对于每个因子,如果它的Sigma函数中连乘的每一项都是偶数,那么整个Sigma函数就是偶数。具体实现中,可以判断每个因子的平方根是否为偶数,如果是偶数,则减去(平方根+1)/2。\[1\] 另外,还可以使用O(1)的做法来解决这个问题。根据观察,所有的完全平方数及其两倍的值都会导致Sigma函数为偶数。因此,可以直接计算n的平方根,然后减去(平方根+1)/2即可得到结果。\[3\] #### 引用[.reference_title] - *1* [Sigma Function](https://blog.csdn.net/PNAN222/article/details/50938232)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [【LightOJ1336】Sigma Function(数论)](https://blog.csdn.net/qq_30974369/article/details/79009498)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值