码迷,mamicode.com
首页 > Windows程序 > 详细

【BZOJ1026】Windy数

时间:2017-03-22 12:43:00      阅读:307      评论:0      收藏:0      [点我收藏+]

标签:type   输出   and   output   init   ima   submit   typedef   image   

1026: [SCOI2009]windy数

Time Limit: 1 Sec  Memory Limit: 162 MB
Submit: 7195  Solved: 3238
[Submit][Status][Discuss]

Description

  windy定义了一种windy数。不含前导零且相邻两个数字之差至少为2的正整数被称为windy数。 windy想知道,
在A和B之间,包括A和B,总共有多少个windy数?

Input

  包含两个整数,A B。

Output

  一个整数

Sample Input

【输入样例一】
1 10
【输入样例二】
25 50

Sample Output

【输出样例一】
9
【输出样例二】
20

HINT

 

【数据规模和约定】

100%的数据,满足 1 <= A <= B <= 2000000000 。

 

Source

 

Sol:

重学数位dp

设$f[i][j]$表示当前这个数有i位,且第i位为j的方案数

那么显然转移是$f[i][j]=f[i-1][k] (|j-k| \ge 2)$

假如这个数有$len$位

统计答案时,我们先统计第$len$位内所有的比它第一位小的并且不是0的数的个数

然后我们枚举第$len-1$位到第$1$位的所有数 然后计算一下

然后这个时候第一位固定了 我们枚举剩下的位数 假如不合法 立刻退出 

技术分享
/*To The End Of The Galaxy*/
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<iomanip>
#include<bitset>
#include<stack>
#include<map>
#include<set>
#include<cmath>
#include<complex>
#define debug(x) cerr<<#x<<"="<<x<<endl
#define INF 0x7f7f7f7f
#define llINF 0x7fffffffffffll
#define P(x,y) (((x-1)*c)+y)
using namespace std;
typedef pair<int,int> pii;
typedef long long ll;
inline int init()
{
    int now=0,ju=1;char c;bool flag=false;
    while(1)
    {
        c=getchar();
        if(c==-)ju=-1;
        else if(c>=0&&c<=9)
        {
            now=now*10+c-0;
            flag=true;
        }
        else if(flag)return now*ju;
    }
}
inline long long llinit()
{
    long long now=0,ju=1;char c;bool flag=false;
    while(1)
    {
        c=getchar();
        if(c==-)ju=-1;
        else if(c>=0&&c<=9)
        {
            now=now*10+c-0;
            flag=true;
        }
        else if(flag)return now*ju;
    }
}
ll f[30][10];
void calc()
{
    for(int i=0;i<=9;i++)
    {
        f[1][i]=1;
    }
    for(int i=2;i<=20;i++)
    {
        for(int j=0;j<=9;j++)
        {
            for(int k=0;k<=9;k++)
            {
                if(abs(j-k)>=2)
                {
                    f[i][j]+=f[i-1][k];
                }
            }
        }
    }
}
int num[20];
int cnt=0;
ll solve(ll x)
{
    int cnt=0;ll ans=0;
    while(x)
    {
        num[++cnt]=x%10;
        x/=10;
    }
    for(int i=1;i<cnt;i++)
    {
        for(int j=1;j<=9;j++)
        {
            ans+=f[i][j];
        }
    }
    for(int i=1;i<num[cnt];i++)
    {
        ans+=f[cnt][i];
    }
    for(int i=cnt-1;i>=1;i--)
    {
        for(int j=0;j<num[i];j++)
        {
            if(abs(num[i+1]-j)>=2)ans+=f[i][j];
        }
        if(abs(num[i]-num[i+1])<2)break;
    }
    return ans;
}
#ifdef unix
    #define LLD "%lld"
#else
    #define LLD "%I64d"
#endif
int main()
{
    ll a,b;
    calc();
    a=llinit();b=llinit();
    printf(LLD,solve(b+1)-solve(a));
    return 0;
}
View Code

 

【BZOJ1026】Windy数

标签:type   输出   and   output   init   ima   submit   typedef   image   

原文地址:http://www.cnblogs.com/redwind/p/6599027.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!