SPOJ BALNUM Balanced Numbers(数位dp)

Balanced Numbers

Time Limit:123MS     Memory Limit:1572864KB     64bit IO Format:%lld
& %llu

Submit Status Practice SPOJ
BALNUM

Description

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 <= 1019

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

题意:求所给区间中,数位中,奇数的出现偶数次,偶数的出现技术次的个数。

题解:看了别人写的题解才知道,把数压进三进制中,0代表没有,1:奇数次

2:偶数次

比如:

126099这个数:0  1  2  3  4  5  6  7  8  9(三进制中的位数)

1  1  1   0  0  0  1  0  0  2(表示)

于是s=3^0*1+3^1*1+3^2*1+3^6*1+3^9*2

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<cmath>
#define ll long long
#define N 1010
using namespace std;

int n;

ll l,r;
int dp[22][58888];
int num[22];
int p3[20];

void init() {
    p3[0]=1;
    for(int i=1; i<=10; i++)
        p3[i]=p3[i-1]*3;
}

int new_s(int s,int d) {
    int ss=0;
    int b[12];
    memset(b,0,sizeof b);
    int l=0;
    while(s) {
        b[l++]=s%3;
        s/=3;
    }
    b[d]+=1;
    if(b[d]==3)b[d]=1;///出现3次,奇数次,变为1
    for(int i=0; i<=9; i++)
        ss+=p3[i]*b[i];
    return ss;
}

///判断是不是每一位数是否满足:奇数的都出现欧数次,偶数的都出现欧数次
bool judge(int s) {
    for(int i=0; i<=9; i++) {
        int x=s%3;
        s/=3;
        if(x==0)continue;    ///没出现

        ///不符合:奇数的都出现欧数次,偶数的都出现欧数次
        if(x==1&&(i&1))return 0;
        if(x==2&&!(i&1))return 0;
    }
    return 1;
}

///模板
int dfs(int i,int s,bool e,bool lze) {
    if(i==-1)return lze?0:judge(s);
    if(!e&&dp[i][s]!=-1)return dp[i][s];
    ll res=0;
    int u=e?num[i]:9;
    for(int d=0; d<=u; d++) {
        if(lze) {///是前导0
            if(d==0)res+=dfs(i-1,s,e&&d==u,lze);
            else   res+=dfs(i-1,new_s(s,d),e&&d==u,lze&&d==0);
        } else
            res+=dfs(i-1,new_s(s,d),e&&d==u,lze&&d==0);
    }
    return (e)?res:dp[i][s]=res;
}

int solve(ll x) {
    int len=0;
    while(x) {
        num[len++]=x%10;
        x/=10;
    }
    return dfs(len-1,0,1,1);
}

int main() {
    //freopen("in.txt","r",stdin);
    cin>>n;
    init();
    memset(dp,-1,sizeof dp);
    while(n--) {
        scanf("%lld%lld",&l,&r);
        printf("%d\n",solve(r)-solve(l-1));
    }
    return 0;
}
时间: 2024-10-18 16:06:45

SPOJ BALNUM Balanced Numbers(数位dp)的相关文章

SPOJ10606 BALNUM - Balanced Numbers(数位DP+状压)

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 numb

spoj 10606 Balanced Numbers 数位dp

题目链接 一个数称为平衡数, 满足他各个数位里面的数, 奇数出现偶数次, 偶数出现奇数次, 求一个范围内的平衡数个数. 用三进制压缩, 一个数没有出现用0表示, 出现奇数次用1表示, 出现偶数次用2表示, 这样只需要开一个20*60000的数组. 1 #include<bits/stdc++.h> 2 using namespace std; 3 #define pb(x) push_back(x) 4 #define ll long long 5 #define mk(x, y) make_

SPOJ BALNUM Balanced Numbers 状压+数位DP

一开始想了一个用二进制状压的方法,发现空间需要的太大,光光memset都要超时 = = 其实不用每次都memset 也可以用三进制,一开始直接打表出所有的状态转移就好 #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostream&g

Balanced Numbers数位dp

三进制搞下, 0  表示没出现过,  第i位为1 表示 i出现了奇数次,  2表示i 出现了偶数次. #include <cstdio> #include <cstring> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #include <cstdlib> #includ

SPOJ BALNUM Balanced Numbers(数位dp,状态压缩)

BALNUM - Balanced Numbers no tags 题目链接 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)   

SPOJ - BALNUM - Balanced Numbers(数位DP)

链接: https://vjudge.net/problem/SPOJ-BALNUM 题意: 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)

spoj Balanced Numbers(数位dp)

一个数字是Balanced Numbers,当且仅当组成这个数字的数,奇数出现偶数次,偶数出现奇数次 一下子就相到了三进制状压,数组开小了,一直wa,都不报re, 使用记忆化搜索,dp[i][s] 表示长度为i,状态为s,时,满足条件的balance number的个数 #include <stdio.h> #include <string.h> #include <stdlib.h> #include <algorithm> #include <io

SPOJ BALNUM Balanced Numbers 平衡数(数位DP,状压)

题意: 平衡树定义为“一个整数的某个数位若是奇数,则该奇数必定出现偶数次:偶数位则必须出现奇数次”,比如 222,数位为偶数2,共出现3次,是奇数次,所以合法.给一个区间[L,R],问有多少个平衡数? 思路: 这题比较好解决,只有前导零问题需要解决.如果枚举到011,那么其前导零(偶数)出现了1次而已,而此数11却是平衡数,所以不允许前导零的出现! 由于dfs时必定会枚举到前导零,否则位数较少的那些统计不到.状态需要3维or2维也行,3维的比较容易处理,用一维表示数位出现次数,另一维表示数位是否

[SPOJ BALNUM] Balanced Numbers

dp[dep][ex][sta]表示长度为dep的,前面出现过的数的集合为ex,不满足要求的数字的集合为sta的满足要求的数的个数 1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 ll dp[20][1024][1024]; 5 int dig[20],tmp; 6 ll dfs(int dep,int ex,int sta,int zero,int flag){ 7 if(!dep)retu