数位DP的感觉,但是跟模版不是一个套路的,看的题解,代码好理解,但是确实难想。
1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 using namespace std; 5 #define LL long long 6 LL dp[31][2][2][2]; 7 int a[31],b[31],c[31]; 8 void fun(int *p,int x) 9 { 10 int i; 11 for(i = 0; i <= 30; i ++) 12 { 13 if(x&(1<<i)) 14 p[i] = 1; 15 else 16 p[i] = 0; 17 } 18 } 19 LL dfs(int pos,int ta,int tb,int tc) 20 { 21 if(pos == -1) 22 return 1; 23 LL & res = dp[pos][ta][tb][tc]; 24 int x,y,z; 25 if(res == -1) 26 { 27 res = 0; 28 for(x = 0; x < 2; x ++) 29 { 30 for(y = 0; y < 2; y ++) 31 { 32 z = x^y; 33 if((!ta||(x <= a[pos]))&&(!tb||(y <= b[pos]))&&(!tc||(z <= c[pos]))) 34 { 35 res += dfs(pos-1,ta&&(x == a[pos]),tb&&(y == b[pos]),tc&&(z == c[pos])); 36 } 37 } 38 } 39 } 40 return res; 41 } 42 class LittleElephantAndXor 43 { 44 public : 45 LL getNumber(int A, int B, int C) 46 { 47 memset(dp,-1,sizeof(dp)); 48 fun(a,A); 49 fun(b,B); 50 fun(c,C); 51 return dfs(30,1,1,1); 52 } 53 };