- #include <bits/stdc++.h>
- using namespace std;
- #define ll long long int
- vector <int> bits;
- ll DP[35][2][2][35];
- ll dp(ll pos, ll prev,ll issmall,ll couple){
- if(pos == bits.size()){
- return couple;
- }
- if(DP[pos][prev][issmall][couple]!=-1){
- return DP[pos][prev][issmall][couple];
- }
- ll ret = 0;
- ll mx = 1;
- if(!issmall) mx = bits[pos];
- for(int i = 0; i <=mx; i++){
- ret+= dp(pos+1,i, (issmall|(i<bits[pos])),((prev==1 &&i==1)+ couple));
- }
- DP[pos][prev][issmall][couple] = ret;
- return ret;
- }
- ll convert(ll n)
- {
- bits.clear();
- while(n!= 0){
- bits.push_back(n%2);
- n = n/2;
- }
- reverse(bits.begin(), bits.end());
- return dp(0, 0, 0, 0);
- }
- int main()
- {
- ll ts;
- cin>>ts;
- for(int i = 1; i <= ts; i++){
- ll n;
- cin>>n;
- memset(DP, -1, sizeof(DP));
- ll ans = convert(n);
- cout<<"Case "<<i<<": "<<ans<<endl;
- }
- return 0;
- }
Thursday 27 July 2017
Lightoj 1032 - Fast Bit Calculations
Subscribe to:
Post Comments (Atom)
Most Featured Post
Lightoj 1159 - Batman
http://lightoj.com/volume_showproblem.php?problem=1159 problem analysis: First i thought of this as if s1, s2 and s3 are those three str...
-
Problem link: Problem Analysis: It is actually a basic Bisection problem , as we can see here we can not actually find a formula fo...
-
http://lightoj.com/volume_showproblem.php?problem=1382 Problem analysis: This is a rare problem i wrote about so far. After much strugg...
Thank you for this post. Readable nice code.
ReplyDeleteHappy to help :)
DeleteThank you for your meaning code
ReplyDeleteHappy to help :)
Delete