Balanced Numbers 数位DP+3进制压缩
来源:互联网 发布:王陆807和语料库 知乎 编辑:程序博客网 时间:2024/05/16 04:41
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 bothA 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:21 10001 9
Output:1474
题意:求范围内 偶数出现奇数次,奇数出现偶数次的 数的 个数
思路:数位DP+3进制压缩。有3种状态(0:没出现过, 1:数字出现奇数次, 2:数字出现偶数次), 0~9 出现的次数就可以用3进制表示,最大的数就是 310 ,那么就可以把1019 哈希到310 内。dp[pos][sta]含义即 pos位的数字中有sta种方法数(hash过后的)。
Code:
#include <iostream>#include <string.h>#include <cstdio>#define LL long long using namespace std;int a[25];LL dp[25][60000];bool check( LL x ){for( int i = 0 ; i < 10 ; i++ ){if( (i&1) && (x%3==1) ) return false ;if( !(i&1) && (x%3==2) ) return false ;x /= 3;}return true;}LL hash( int sta , int i ){ //作用就是把i这个数字出现的次数奇偶性改变 得到新的sta(方法数)。int b[10]; for( int j = 0 ; j < 10 ; j++ ){b[j] = sta % 3 ; //将sta转化3进制存储在b数组,分别对应每个数字出现的奇偶次数。sta /= 3; }if( b[i] == 0 ) b[i] = 1 ; //改变 i 的出现次数奇偶性(0:没出现过, 1:数字出现奇数次, 2:数字出现偶数次)else b[i] = 3 - b[i];for( int j = 9 ; j >= 0 ; j -- ) sta = sta * 3 + b[j]; //得到新的sta值return sta;}LL dfs( int pos , int sta , bool lead , bool limit ){if( pos == -1 ) return check(sta) ;if( !limit && dp[pos][sta] != -1 ) return dp[pos][sta];LL ans = 0 ;int up = limit ? a[pos] : 9 ;for( int i = 0 ; i <= up ; i++ ){if( lead && i == 0 ) ans += dfs( pos - 1 , sta , lead && i == 0 , limit && i == up);else ans += dfs( pos - 1 , hash( sta , i ) , lead && i == 0 , limit && i == up);}if( !limit ) dp[pos][sta] = ans ;return ans ;}LL solve( LL x ){int tot = 0 ;while( x ){a[tot++] = x % 10;x /= 10 ;}return dfs( tot - 1 , 0, true, true );}int main(){ios_base::sync_with_stdio(false); cin.tie(0);int T;cin >> T ;int l , r ;memset(dp,-1,sizeof(dp));while( T-- ){cin >> l >> r ;cout << solve(r) - solve(l-1) << endl;}return 0;}
- Balanced Numbers 数位DP+3进制压缩
- SPOJ BALNUM Balanced Numbers(数位DP + 状态压缩)
- 【SPOJ-BALNUM】Balanced Numbers【数位DP】【状态压缩】
- [kuangbin带你飞]专题十五 数位DP K - Balanced Numbers (数位dp)(进制转换)
- SPOJ - BALNUM Balanced Numbers数位DP+3进制存图
- SPOJ Balanced Numbers (数位DP+3进制状压)【模板】
- [数位dp] spoj 10606 Balanced Numbers
- SPOJ10606---Balanced Numbers(三进制数位dp)
- 【数位DP】SPOJ 10606 BALNUM Balanced Numbers
- SPOJ BALNUM Balanced Numbers(数位dp)
- spoj 10606 BALNUM - Balanced Numbers 数位dp
- SPOJ BALNUM Balanced Numbers (数位dp)
- 【动态规划】【数位DP】[SPOJ10606]Balanced numbers
- SPOJ 10606 Balanced Numbers - 数位dp
- SPOJ-BALNUM Balanced Numbers (数位DP)
- SPOJ BALNUM - Balanced Numbers(数位DP)
- SPOJ Balanced Numbers(数位dp,三进制状压)
- [SPOJ BALNUM - Balanced Numbers]数位DP
- JavaScript基础
- MySQL的InnoDB索引原理详解
- 界面编码报错:Page-encoding specified in XML prolog (UTF-8) is different from that specified in
- python学习十(多继承,多态,异常)
- Mybatis--更新
- Balanced Numbers 数位DP+3进制压缩
- 调用百度翻译接口demo(完整ajax请求)
- windows下使用pthread
- 手把手教你Android手机与BLE终端通信--搜索
- 用quartz进行定时任务的开发
- 系统监控工具
- 在进行C#编程时候,有的时候我们需要判断一个字符串是否是数字字符串,我们可以通过以下两种方法来实现。 【方法一】:使用 try{} catch{} 语句。 我们可以在try语句块中试图
- 第 8 章 查找
- poj2528(线段树区间染色)