【POJ 3252】 Round Numbers

【POJ 3252】 Round Numbers

组合数学里的题。。在我飞的引导下走上了数位dp的不归路。。。这样算不算开挂。。。。好羞涩

数位dp真的真的真的好好用!!!

数位dp真的真的真的好好用!!!

数位dp真的真的真的好好用!!!

重要的事说三遍 一入数位dp深似海 再也粗不来了。。。 用数位很好想……都不知道该怎么写题解。。。

代码如下:

#include <iostream>
#include <cstdio>
#include <cstring>
#define ll long long

using namespace std;

int dp[31][2][63];
int digit[31];

/*
big 0 1 平衡 防止负从31开始
hs 前导0
*/

int dfs(int pos,int big,bool hs,bool high)
{
    if(pos == -1) return big >= 31;
    if(!high && ~dp[pos][hs][big]) return dp[pos][hs][big];

    int i,en,ans = 0,nbig;
    bool nhs;
    en = high? digit[pos]: 1;
    for(i = 0; i <= en; ++i)
    {
        nbig = big, nhs = hs;
        if(i)
        {
            nbig--;
            nhs = 0;
        }
        else if(!hs) nbig++;

        ans += dfs(pos-1,nbig,nhs,high && i == en);
    }

    if(!high) dp[pos][hs][big] = ans;
    return ans;
}

int Solve(ll x)
{
    int len = 0;
    while(x)
    {
        digit[len++] = x%2;
        x >>= 1;
    }
    return dfs(len-1,31,1,1);
}

int main()
{
    memset(dp,-1,sizeof(dp));
    ll st,en;/////////////////这里必须吐槽下…………今天不宜敲代码 上午数组开小了 hdoj的题 不断WA 这次还好 ll开成int 最关键输入还用的%lld。。。然后给了RE 找了半天突然发现了TOT
    scanf("%lld %lld",&st,&en);
    printf("%d\n",Solve(en) - Solve(st-1));
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-09 05:35:37

【POJ 3252】 Round Numbers的相关文章

【POJ 2942】Knights of the Round Table(双联通分量+染色判奇环)

[POJ 2942]Knights of the Round Table(双联通分量+染色判奇环) Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 11661   Accepted: 3824 Description Being a knight is a very attractive career: searching for the Holy Grail, saving damsels in distress, an

【POJ 1584】 A Round Peg in a Ground Hole (判凸包+判圆在凸包内)

[POJ 1584] A Round Peg in a Ground Hole (判凸包+判圆在凸包内) 这题题面是一大坑..长长的 明显是给我这种英语渣准备的... 大体意思是给出一个多边形的点 按顺时针或逆时针给出 判断是否为凸包 同时给出一个圆(圆心坐标+半径) 问这个圆在不在多边形内 首先顺逆时针不确定 我的做法是输入时先判断顺时针还是逆时针输入 然后统统变成逆时针来走 就是根据两种情况传入不同的枚举起点 终点 和加减(具体见代码 判凸包用建凸包的叉成法即可 既然逆时针走 那么如果是凸包

【POJ 2750】 Potted Flower(线段树套dp)

[POJ 2750] Potted Flower(线段树套dp) Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4566   Accepted: 1739 Description The little cat takes over the management of a new park. There is a large circular statue in the center of the park, surrou

【POJ 3321】 Apple Tree (dfs重标号设区间+树状数组求和)

[POJ 3321] Apple Tree (dfs重标号设区间+树状数组求和) Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 21966   Accepted: 6654 Description There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow in the tree. K

【POJ 3440】 Coin Toss(概率公式)

[POJ 3440] Coin Toss(概率公式) Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 3591   Accepted: 957 Description In a popular carnival game, a coin is tossed onto a table with an area that is covered with square tiles in a grid. The prizes ar

【POJ 3071】 Football(DP)

[POJ 3071] Football(DP) Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4350   Accepted: 2222 Description Consider a single-elimination football tournament involving 2n teams, denoted 1, 2, -, 2n. In each round of the tournament, all tea

【POJ 1151】 Atlantis(离散化+扫描线)

[POJ 1151] Atlantis(离散化+扫描线) Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 20223   Accepted: 7634 Description There are several ancient Greek texts that contain descriptions of the fabled island Atlantis. Some of these texts even inclu

【POJ 2411】Mondriaan&#39;s Dream(状压dp)

[POJ 2411]Mondriaan's Dream(状压dp) Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 14107   Accepted: 8152 Description Squares and rectangles fascinated the famous Dutch painter Piet Mondriaan. One night, after producing the drawings in hi

【POJ 1739】Tony&#39;s Tour

Tony's Tour Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 3545   Accepted: 1653 Description A square township has been divided up into n*m(n rows and m columns) square plots (1<=N,M<=8),some of them are blocked, others are unblocked.