[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)return (ex&sta)==0;
 8     if(!flag&&dp[dep][ex][sta]!=-1)return dp[dep][ex][sta];
 9     int lim=flag?dig[dep]:9;
10     ll ans=0;
11     for(int i=0;i<=lim;i++){
12         if(zero&&i==0)ans+=dfs(dep-1,0,tmp,1,flag&(i==lim));
13         else ans+=dfs(dep-1,ex|(1<<i),sta^(1<<i),0,flag&(i==lim));
14     }
15     if(!flag)dp[dep][ex][sta]=ans;
16     return ans;
17 }
18 ll solve(ll x){
19     int dd=0;
20     while(x)dig[++dd]=x%10,x/=10;
21     return dfs(dd,0,tmp,1,1);
22 }
23 int main(){
24     memset(dp,-1,sizeof(dp));
25     for(int i=0;i<10;i+=2)tmp|=(1<<i);
26     int T;
27     scanf("%d",&T);
28     while(T--){
29         ll A,B;
30         scanf("%lld%lld",&A,&B);
31         printf("%lld\n",solve(B)-solve(A-1));
32     }
33     return 0;
34 }

时间: 2024-08-30 07:06:21

[SPOJ BALNUM] Balanced Numbers的相关文章

SPOJ BALNUM Balanced Numbers 状压+数位DP

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

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)

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

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 BALNUM Balanced Numbers 平衡数(数位DP,状压)

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

BALNUM - Balanced Numbers

BALNUM - Balanced Numbers Time limit:123 ms Memory limit:1572864 kB 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

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_

[数位dp] spoj 10606 Balanced Numbers

题意: 对于一个数的每个位上的数. 对于每个奇数,如果出现必须出现偶数次. 对于每个偶数,如果出现必须出现奇数次. 思路: 用三进制存储每个数出现的状态,0没出现,1出现奇数次,2出现偶数次. 然后其他和普通数位dp就一样了. 代码: #include"cstdlib" #include"cstdio" #include"cstring" #include"cmath" #include"queue" #i