zoukankan      html  css  js  c++  java
  • 【BZOJ1026】Windy数

    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
  • 相关阅读:
    mysql_wp_replication_tutorial
    Procedure execution failed 2013
    [Err] 1136
    innodb_flush_log_at_trx_commit和sync_binlog 参数说明
    mysql没有oracle 那样一次性把data buffer 数据写入磁盘
    Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field
    MyEclipse之Widget is disposed
    Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Query was empty
    An internal error occurred during: "Building workspace". GC overhead limit exceeded
    Oracle 内部复制文档解读
  • 原文地址:https://www.cnblogs.com/redwind/p/6599027.html
Copyright © 2011-2022 走看看