标签:sep color mod bit oid while where single 统计
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 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.
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
For each test case, you need to write a number in a single line: the amount of balanced numbers in the corresponding interval
Input: 2 1 1000 1 9
Output: 147 4
分析:如何统计0~9是否出现过且是否出现奇偶次是难点;
正解是三进制压缩,该位置为0代表没出现,1代表出现奇数次,2代表出现偶数次;
不过一看内存这么大,可以随便做了,dp[i][j][k]分别代表位置,二进制判是否出现,二进制判每个数出现次数奇偶性;
注意前导0不算;
代码:
#include <iostream> #include <cstdio> #include <cstdlib> #include <cmath> #include <algorithm> #include <climits> #include <cstring> #include <string> #include <set> #include <bitset> #include <map> #include <queue> #include <stack> #include <vector> #define rep(i,m,n) for(i=m;i<=n;i++) #define mod 1000000007 #define inf 0x3f3f3f3f #define vi vector<int> #define pb push_back #define mp make_pair #define fi first #define se second #define ll long long #define pi acos(-1.0) #define pii pair<int,int> #define sys system("pause") const int maxn=1e5+10; const int N=5e4+10; const int M=N*10*10; using namespace std; inline ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);} inline ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;} inline void umax(ll &p,ll q){if(p<q)p=q;} inline void umin(ll &p,ll q){if(p>q)p=q;} inline ll read() { ll x=0;int f=1;char ch=getchar(); while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();} while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();} return x*f; } int n,m,k,t,num[20],pos; ll dp[20][1<<10][1<<10],p,q; ll dfs(int pos,int x,int y,int z,int k) { if(pos<0) { int i; rep(i,0,9)if(x>>i&1&&(y>>i&1)^(i&1)!=1)return 0; return 1; } if(z&&k&&dp[pos][x][y]!=-1)return dp[pos][x][y]; int now=z?9:num[pos],i; ll ret=0; rep(i,0,now) { ret+=dfs(pos-1,!i&&!k?x:x|(1<<i),!i&&!k?y:y^(1<<i),z||i<num[pos],k||i); } return z&&k?dp[pos][x][y]=ret:ret; } ll gao(ll x) { pos=0; while(x)num[pos++]=x%10,x/=10; return dfs(pos-1,0,0,0,0); } int main() { int i,j; memset(dp,-1,sizeof(dp)); scanf("%d",&t); while(t--) { scanf("%lld%lld",&p,&q); printf("%lld\n",gao(q)-gao(p-1)); } return 0; }
标签:sep color mod bit oid while where single 统计
原文地址:http://www.cnblogs.com/dyzll/p/6390763.html