PDF (English) | Statistics | Forum |
Time Limit: 2 second(s) | Memory Limit: 32 MB |
A bit is a binary digit, taking a logical value of either 1 or 0 (also referred to as "true" or "false" respectively). And every decimal number has a binary representation which is actually a series of bits. If a bit of a number is 1 and its next bit is also 1 then we can say that the number has a 1adjacent bit. And you have to find out how many times this scenario occurs for all numbers up to N.
Examples:
Number Binary Adjacent Bits
12 1100 1
15 1111 3
27 11011 2
Input starts with an integer T (≤ 10000), denoting the number of test cases.
Each case contains an integer N (0 ≤ N < 231).
For each test case, print the case number and the summation of all adjacent bits from 0 to N.
Sample Input |
Output for Sample Input |
7 0 6 15 20 21 22 2147483647 |
Case 1: 0 Case 2: 2 Case 3: 12 Case 4: 13 Case 5: 13 Case 6: 14 Case 7: 16106127360 |
SPECIAL THANKS: JANE ALAM JAN (MODIFIED DESCRIPTION, DATASET)
把数转化成2进制然后按位dp就好
ACcode:
#include <cstdio> #include <cstring> #define ll long long ll dp[37][37][2];///0--->都没有 1--->前面有1 int data[36]; int cnt=1; ll dfs(int len,int num,int flag,int limit){ if(!len) return num; if(!limit&&dp[len][num][flag]!=-1)return dp[len][num][flag]; int ed=limit?data[len]:1; ll ans=0; for(int i=0;i<=ed;++i) if(flag)ans+=dfs(len-1,num+(i==1),i==1,limit&&i==ed); else ans+=dfs(len-1,num,i==1,limit&&i==ed); return limit?ans:dp[len][num][flag]=ans; } void doit(){ ll n; scanf("%lld",&n); int len=0; while(n){ data[++len]=n%2; n>>=1LL; } printf("Case %d: %lld\n",cnt++,dfs(len,0,0,1)); } int main(){ int loop;memset(dp,-1,sizeof(dp)); scanf("%d",&loop); while(loop--)doit(); return 0; }