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) 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.

思路:

三进制记录每个值用的奇数次还是偶数次。
直接DP即可。

代码:

// #include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<vector>
#include<string.h>
#include<set>
#include<queue>
#include<algorithm>
#include<math.h>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const int MOD = 1e9+7;
const int MAXN = 1e6+10;

ULL a, b;
ULL F[21][60000];
int dig[21];
ULL m[11];

int Upd(int x, int p)
{
    int sum = 0;
    for (int i = 0;i < 10;i++)
    {
        int tmp = x%3;
        x /= 3;
        if (i == p)
            sum += (tmp == 1 ? 2 : 1) * m[i];
        else
            sum += tmp * m[i];
    }
    return sum;
}

bool Check(int x)
{
    int p = 0;
    while(x)
    {
        if (x%3 == 2 && p%2 == 0)
            return false;
        if (x%3 == 1 && p%2 == 1)
            return false;
        x /= 3;
        p++;
    }
    return true;
}

ULL Dfs(int pos, int sta, bool zer, bool lim)
{
    if (pos == -1)
        return Check(sta);
    if (!lim && F[pos][sta] != -1)
        return F[pos][sta];
    int up = lim ? dig[pos] : 9;
    ULL ans = 0;
    for (int i = 0;i <= up;i++)
    {
        ans += Dfs(pos-1, (zer && i == 0) ? 0 : Upd(sta, i), zer && i == 0, lim && i == up);
    }
    if (!lim)
        F[pos][sta] = ans;
    return ans;
}

ULL Solve(ULL x)
{
    int p = 0;
    while(x)
    {
        dig[p++] = x%10;
        x /= 10;
    }
    return Dfs(p-1, 0, 1, 1);
}

int main()
{
    // freopen("test.in", "r", stdin);
    m[0] = 1;
    for (int i = 1;i < 11;i++)
        m[i] = m[i-1]*3;
    memset(F, -1, sizeof(F));
    int t;
    scanf("%d", &t);
    while(t--)
    {
        scanf("%llu %llu", &a, &b);
        printf("%llu\n", Solve(b)-Solve(a-1));
    }

    return 0;
}

原文地址:https://www.cnblogs.com/YDDDD/p/12000230.html

时间: 2024-08-01 15:04:53

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

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 n

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