码迷,mamicode.com
首页 > 其他好文 > 详细

lightOJ 1205(Palindromic Numbers数位DP)

时间:2015-08-28 11:08:14      阅读:212      评论:0      收藏:0      [点我收藏+]

标签:lightoj   dp   

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 j (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
Source
Problem Setter: Jane Alam Jan

题意比较简单,我就直接上代码了,一些步骤我有记在代码里;

/* ***********************************************
Author        :xdlove
Created Time  :2015年08月18日 星期二 13时18分54秒
File Name     :xd.cpp
 ************************************************ */

/*
 * lightOJ 1205 (数位DP)
 * dp[i][j] 表示以j开头的i位数
 * 处理比较烦。。。
 */

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;

/********************************

     please don‘t hack me!! /(ToT)/~~

                __------__
              /~          ~             |    //^\\//^\|
           /~~\  ||  T| |T|:~          | |6   ||___|_|_||:|
           \__.  /      o  \/‘
            |   (       O   )
   /~~~~\    `\  \         /
  | |~~\ |     )  ~------~` /‘ |  | |   /     ____ /~~~)(_/‘   | | |     /‘    |    ( |
       | | |     \    /   __)/        \  \ \      \/    /‘ \   `         \  \|\        /   | |\___|
           \ |  \____/     | |
           /^~>  \        _/ <
          |  |         \                 |  | \        \                  -^-\  \       |        )
               `\_______/^\______/

************************************/

#define clr(a) memset(a,0,sizeof(a));
typedef long long ll;
ll dp[20][10];
ll haha;
int tol;
int bit[20],nm[20];

bool check(ll x)
{
    ll n = x;
    ll res = 0;
    while(n)
    {
        res = res * 10 + n % 10;
        n /= 10;
    }
    return x == res;
}

ll fuck(ll l,ll r,int t)
{
    //除了第一位不能取0外,其他位可以取0,用t来进行标记
    ll ans = 0;
    if(l == r) 
    {
        ll tp = 0;
        nm[l] = nm[r] = bit[r];
        for(int i = tol; i >= 1; i--)
            tp = tp * 10 + nm[i];
        if(tp <= haha) //因为我是一位一位的确定,所以最后需要与原先的数进行比较,因为这个坑了我好久 
            return bit[l] + 1;
        return bit[l];
    }
    if(l > r) 
    {
        ll tp = 0;
        for(int i = tol; i >= 1; i--)
            tp = tp * 10 + nm[i];
        if(tp <= haha) return 1;
        return 0;
    }
    for(int i = bit[r] - 1; i >= t; i--)
        ans += dp[r - l + 1][i];
    nm[l] = nm[r] = bit[r];
    return ans + fuck(l + 1,r - 1,0);
}

ll solve(ll n)
{
    ll ans = 1; //0也是回文数
    haha = n;
    if(n < 0) return 0;
    if(n < 10) return n + 1;
    tol = 0;
    while(n) //将n进行拆位
    {
        bit[++tol] = n % 10;
        n /= 10;
    }
    for(int i = tol - 1; i >= 1; i--) // 预先算出每一个位为0的时候所有的回文数
        for(int j = 1; j < 10; j++)
            ans += dp[i][j];
    ans += fuck(1,tol,1); //递归处理,一次确定每一个位的数
    return ans;
}

int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    for(int i = 0; i < 10; i++)
        dp[1][i] = dp[2][i] = 1;
    for(int i = 3; i <= 18; i++)
        for(int j = 0; j < 10; j++)
            for(int k = 0; k < 10; k++)
                dp[i][j] += dp[i - 2][k];
    int T,cnt = 0;
    ll l,r;
    cin>>T;
    while(T--)
    {
        cin>>l>>r;
        if(l > r) swap(l,r);
        printf("Case %d: ",++cnt);
        cout<<solve(r) - solve(l - 1)<<endl;
    }
    return 0;
}

版权声明:追逐心中的梦想,永不放弃! By-xdlove

lightOJ 1205(Palindromic Numbers数位DP)

标签:lightoj   dp   

原文地址:http://blog.csdn.net/zsgg_acm/article/details/48048461

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!