• bunoj 13124(数位dp)


     数位dp每次都给我一种繁琐的感觉。。

    A - Palindromic Numbers
    Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu
    Submit Status

    Description

    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

    4

    1 10

    100 1

    1 1000

    1 10000

    Sample Output

    Case 1: 9

    Case 2: 18

    Case 3: 108

    Case 4: 198

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <algorithm>
    #include <math.h>
    #include <map>
    #include <queue>
    #include <sstream>
    #include <iostream>
    using namespace std;
    #define INF 0x3fffffff
    
    typedef __int64 LL;
    
    LL save[20];
    int bit[20];
    int cnt;
    LL dp[20];
    
    
    LL get(LL x)
    {
        if(x==0) return 1;
        LL tx=x;
        cnt=0;
        while(x)
        {
            bit[++cnt]=x%10;
            x/=10;
        }
        LL ans=0;
        ans=dp[cnt-1];
        for(int i=1;i<bit[cnt];i++)
        {
            if(cnt-2>=0)
                ans+=save[cnt-2];
            else ans++;
        }
        if(cnt==1) return ans+1; //
        int tmp=4;
        for(int i=2;i<=(cnt+1)/2;i++)
        {
            if( cnt-tmp>=0)
                ans += save[cnt-tmp]*bit[cnt-i+1];
            else ans += bit[cnt-i+1];
            tmp+=2;
        }
        int bit1[20];
    
        for(int i=1;i<=(cnt+1)/2;i++)
        {
            bit1[cnt-i+1]=bit[cnt-i+1];
            bit1[i]=bit[cnt-i+1];
        } //
    
        LL sum=0;
        for(int i=cnt;i>=1;i--)
            sum=sum*10+bit1[i];
        if(sum<=tx) ans++;
        return ans;
    }
    
    
    void dfs(int s)
    {
        LL sum=0;
        if(s==1)
        {
            dp[1]=10;
            return ;
        }
        dfs(s-1);
        sum += dp[s-1];
        sum += 9*(save[s-2]);
        dp[s]=sum;
    }
    
    
    int main()
    {
        //freopen("C:\Users\Administrator\Desktop\in.txt","r",stdin);
        //freopen("C:\Users\Administrator\Desktop\in.txt","w",stdout);
        save[0]=1;
        for(int i=1;i<=18;i++)
        {
            LL tmp=1;
            if(i%2==0)
            {
                for(int j=0;j<i/2;j++)
                    tmp*=10;
            }
            else
            {
                for(int j=0;j<i/2+1;j++)
                    tmp*=10;
            }
            save[i]=tmp;
        }
        dp[0]=1;
        dfs(18);
        int T;
        scanf("%d",&T);
        int tt=1;
        int kk=0;
        for(int i=1;i<=100000000;i++)
            kk++;
    
        while(T--)
        {
            LL a,b;
            cin>>a>>b;
            if(a>b) swap(a,b);
            if(a==0)
            {
                a=0;
            }
            else
                a=get(a-1);
            b=get(b);
            cout<<"Case "<<tt++<<": ";
            cout<<b-a<<endl;
        }
        return 0;
    }
  • 相关阅读:
    php用redis保存session
    php字符串常用算法--字符串加密解密
    PHP file_get_contents设置超时处理方法
    php栈数据结构和括号匹配算法
    JavaScript 函数参数传递到底是值传递还是引用传递
    JavaScript数据操作--原始值和引用值的操作本质
    JavaScript数据类型--值类型和引用类型
    js基本数据类型和typeof
    JS获取字符串实际长度(包含汉字)
    sublime的一些插件
  • 原文地址:https://www.cnblogs.com/chenhuan001/p/3350119.html
Copyright © 2020-2023  润新知