题目链接:
2014 GCJ Round 1B New Lottery Game
题意:
求 x≤A,y≤B ,并且 x &
数据范围:
分析:
官方题解在这里:Here。通篇读下来很有意思,对于理解数位dp很有帮助。
一般的数位dp我们在dfs
的时候只记录一个 limit 表示是否达到区间上限,这里我们需要记录三个,因为x,y和二进制与出来的值都有上限。
// GCJ 2014 #Round1B New Lottery Game
//https://code.google.com/codejam/contest/2994486/dashboard#s=p1&a=1#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
using namespace std;
typedef long long ll;int T, cases = 0;
ll A, B, K;
ll vis[35][3][3][3];ll solve(int cur, int lessA, int lessB, int lessK)
{if (cur == -1) return lessA && lessB && lessK;if (vis[cur][lessA][lessB][lessK] != -1) return vis[cur][lessA][lessB][lessK]; int MaxA = lessA || ((A >> cur) & 1);int MaxB = lessB || ((B >> cur) & 1);int MaxK = lessK || ((K >> cur) & 1);ll ret = solve(cur - 1, MaxA, MaxB, MaxK); // 0 & 0 = 0 if (MaxA) ret += solve(cur - 1, lessA, MaxB, MaxK); // 0 & 1 = 0 if (MaxB) ret += solve(cur - 1, MaxA, lessB, MaxK); // 1 & 0 = 0 if (MaxA && MaxB && MaxK) ret += solve(cur - 1, lessA, lessB, lessK); // 0 & 0 = 0return vis[cur][lessA][lessB][lessK] = ret;
}int main()
{scanf("%d", &T);while (T--) {scanf("%lld%lld%lld", &A, &B, &K);memset(vis, -1, sizeof(vis));printf("Case #%d: %lld\n", ++cases, solve(31, 0, 0, 0));}return 0;
}