ACdream 1154 Lowbit Sum (数位dp)

来源:互联网 发布:mac 远程桌面 右键 编辑:程序博客网 时间:2024/05/16 14:02

题目链接:
ACdream 1154

题解:数位dp
这题可以加深对 lowbit 的理解啊。
你打个表就可以发现规律了。
n 为偶数时,dp[n]=dp[n2]+n2
n 为奇数时,dp[n]=dp[n2]+n2+1

AC代码:

/** this code is made by LzyRapx* Problem: 1154* Verdict: Accepted* Submission Date: 2017-06-21 23:56:51* Time: 180MS* Memory: 1664KB*/#include <bits/stdc++.h>using namespace std;typedef long long ll;int lowbit(int x){    return x&(-x);}ll DP(int x){    if(x==1)return 1;    return 2*DP(x>>1) + (x>>1) + (x&1); }int main(){    int n =0 ;    /*   for(int n=1;n<=10;n++){       int  ans  = 0 ;      for(int i=1;i<=n;i++){          ans+=lowbit(i);          cout<<lowbit(i)<<" ";       }      cout<<endl;      cout<<ans<<endl;  }   */    while(~scanf("%d",&n))    {        cout<<DP(n)<<endl;    }    return 0;}