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

Palindrome Function

时间:2017-08-25 15:03:03      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:cal   ons   climits   ace   discuss   cli   big   fun   tput   

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=L∑rj=lf(i,j)∑i=LR∑j=lrf(i,j) . 
When representing the k-base(k>10) number,we need to use A to represent 10,B to 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≤L≤R≤109,2≤l≤r≤361≤T≤105,1≤L≤R≤109,2≤l≤r≤36) 
Output 
For each test case, output the answer in the form of “Case #i: ans” in a seperate line. 
Sample Input 

1 1 2 36 
1 982180 10 10 
496690841 524639270 5 20 
Sample Output 
Case #1: 665 
Case #2: 1000000 
Case #3: 447525746

分析:很好的数位dp的练习;

   dp[pos][len][base]表示当前在pos位置,len长度,base进制下的状态;

   dfs过程时只需pos小于长度一半时check一下即可;

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <bitset>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <cassert>
#include <ctime>
#define rep(i,m,n) for(i=m;i<=(int)n;i++)
#define inf 0x3f3f3f3f
#define mod 1000000007
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define sys system("pause")
#define ls (rt<<1)
#define rs (rt<<1|1)
#define all(x) x.begin(),x.end()
const int maxn=1e5+10;
const int N=1e4+10;
using namespace std;
ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
ll qmul(ll p,ll q,ll mo){ll f=0;while(q){if(q&1)f=(f+p)%mo;p=(p+p)%mo;q>>=1;}return f;}
ll qpow(ll p,ll q,ll mo){ll f=1;while(q){if(q&1)f=f*p%mo;p=p*p%mo;q>>=1;}return f;}
int n,m,k,t,num[40],tmp[40],cas;
ll dp[40][40][40];
ll dfs(int pos,int len,int bs,int zero,int lim)
{
    if(pos<0)return zero;
    if(zero&&lim&&dp[pos][len][bs]!=-1)return dp[pos][len][bs];
    ll ret=0;
    int ma=lim?bs-1:num[pos],i;
    rep(i,0,ma)
    {
        if(!zero&&i==0)ret+=dfs(pos-1,len-1,bs,zero,lim||i<num[pos]);
        else if(pos>=(len+1)/2)
        {
            tmp[pos]=i;
            ret+=dfs(pos-1,len,bs,zero||i,lim||i<num[pos]);
        }
        else if(pos<(len+1)/2&&i==tmp[len-pos])
        {
            ret+=dfs(pos-1,len,bs,zero||i,lim||i<num[pos]);
        }
    }
    return zero&&lim?dp[pos][len][bs]=ret:ret;
}
ll gao(ll x,int l,int r)
{
    ll ret=0;
    ll y=x;
    int i;
    rep(i,l,r)
    {
        x=y;
        int pos=0;
        while(x)num[pos++]=x%i,x/=i;
        ll p=dfs(pos-1,pos-1,i,0,0);
        ret+=p*i+y-p;
    }
    return ret;
}
int main(){
    int i,j;
    memset(dp,-1,sizeof(dp));
    scanf("%d",&t);
    while(t--)
    {
        ll L,R,l,r;
        scanf("%lld%lld%lld%lld",&L,&R,&l,&r);
        --L;
        printf("Case #%d: %lld\n",++cas,gao(R,l,r)-gao(L,l,r));
    }
    return 0;
}

Palindrome Function

标签:cal   ons   climits   ace   discuss   cli   big   fun   tput   

原文地址:http://www.cnblogs.com/dyzll/p/7427823.html

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