• Balanced Numbers (数位DP)


    Balanced Numbers 

    https://vjudge.net/contest/287810#problem/K

    Balanced numbers have been used by mathematicians for centuries. A positive integer is considered a balanced number if:

    1) Every even digit appears an odd number of times in its decimal representation

    2) Every odd digit appears an even number of times in its decimal representation

    For example, 77, 211, 6222 and 112334445555677 are balanced numbers while 351, 21, and 662 are not.

    Given an interval [A, B], your task is to find the amount of balanced numbers in [A, B] where both A and B are included.

    Input

    The first line contains an integer T representing the number of test cases.

    A test case consists of two numbers A and B separated by a single space representing the interval. You may assume that 1 <= A <= B <= 10 19 

    Output

    For each test case, you need to write a number in a single line: the amount of balanced numbers in the corresponding interval

    Example

    Input:
    2
    1 1000
    1 9
    Output:
    147
    4

    用三进制来计算数位出现的奇偶
     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 #define lson l,mid,rt<<1
     4 #define rson mid+1,r,rt<<1|1
     5 #define sqr(x) ((x)*(x))
     6 #define pb push_back
     7 #define eb emplace_back
     8 #define maxn 13000005
     9 #define eps 1e-8
    10 #define pi acos(-1.0)
    11 #define rep(k,i,j) for(int k=i;k<j;k++)
    12 typedef long long ll;
    13 typedef pair<int,int> pii;
    14 typedef pair<long long,int>pli;
    15 typedef pair<int,char> pic;
    16 typedef pair<pair<int,string>,pii> ppp;
    17 typedef unsigned long long ull;
    18 const long long MOD=1e9+7;
    19 /*#ifndef ONLINE_JUDGE
    20         freopen("1.txt","r",stdin);
    21 #endif */
    22 
    23 ll dp[25][60005];
    24 int a[25];
    25 int k;
    26 
    27 bool Check(int x){
    28     int num[10];
    29     for(int i=0;i<10;i++){
    30         num[i]=x%3;
    31         x/=3;
    32     }
    33     for(int i=0;i<10;i++){
    34         if(num[i]==1&&(i%2)){
    35             return false;
    36         }
    37         if(num[i]==2&&(i%2==0)){
    38             return false;
    39         }
    40     }
    41     return true;
    42 }
    43 
    44 int Change(int x,int v){
    45     int num[10];
    46     for(int i=0;i<10;i++){
    47         num[i]=x%3;
    48         x/=3;
    49     }
    50     if(num[v]==1) num[v]=2;
    51     else if(num[v]==2||num[v]==0) num[v]=1;
    52     x=0;
    53     for(int i=9;i>=0;i--){
    54         x=x*3+num[i];
    55     }
    56     return x;
    57 }
    58 
    59 ll dfs(int pos,int st,int lead,int limit){///要去掉前导0
    60     if(pos==-1) return Check(st);
    61     if(!limit&&dp[pos][st]!=-1) return dp[pos][st];
    62     ll ans=0;
    63     int up=limit?a[pos]:9;
    64     for(int i=0;i<=up;i++){
    65         ans+=dfs(pos-1,(lead==1&&i==0)?0:Change(st,i),lead&&i==0,limit&&i==a[pos]);
    66     }
    67     if(!limit) dp[pos][st]=ans;
    68     return ans;
    69 }
    70 
    71 ll solve(ll x){
    72     int pos=0;
    73     while(x){
    74         a[pos++]=x%10;
    75         x/=10;
    76     }
    77     ll ans=dfs(pos-1,0,1,1);
    78     return ans;
    79 }
    80 
    81 int main(){
    82     #ifndef ONLINE_JUDGE
    83      //   freopen("1.txt","r",stdin);
    84     #endif
    85     std::ios::sync_with_stdio(false);
    86     int t;
    87     ll n,m;
    88     memset(dp,-1,sizeof(dp));
    89     cin>>t;
    90     for(int _=1;_<=t;_++){
    91         cin>>n>>m;
    92         ll ans=solve(m)-solve(n-1);
    93         cout<<ans<<endl;
    94     }
    95 
    96 }
    View Code
     
  • 相关阅读:
    Android实例-Delphi在运行时更改Android屏幕旋转(IOS也支持,不过我可没有苹果机,测试不了)
    delphi实现电脑屏幕旋转(电脑屏幕,不是手机屏幕)
    教程-关于escape和URI之间的不同!
    在Delphi中URLEncode文件名的最佳方法是什么?
    Delphi实现js中的escape()编码和unescape()解码
    面向对象: 接口与对象生存周期,接口自动释放
    问题-Delphi在做窗体派生时提示Resource TForm2 not found
    问题-delphi idTCPserver-Socket error问题详解
    delphi 求两个时间差
    Delphi 解决StrToDateTime()不是有效日期类型的问题
  • 原文地址:https://www.cnblogs.com/Fighting-sh/p/10527045.html
Copyright © 2020-2023  润新知