[SCOI2009]windy数

[SCOI2009]windy数

Time Limit:1000MS  Memory Limit:165536K
Total Submit:118 Accepted:54

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

【数据规模和约定】
20%的数据,满足 1 <= A <= B <= 1000000 。
100%的数据,满足 1 <= A <= B <= 2000000000 。

Source

Day1
哎。。还是数位统计问题,幸好对付这种题目可以写个暴力程序来对拍,不然肯定要悲剧啊囧。。。有经验了。。总结一下:
首先要注意第一位就是0的情况,着并不代表这位是0,而是忽略这位,所以按照公式写个特殊的统计的就OK了。。
还有就是可以前面的数位已经不能是windy数了,就不要推下去了。。
同时一定要写个暴力对拍。。
对拍要多考虑这样的数据:
1: 1-1000000这样的.。。
2:1-99999这样的。。
3: xxx-xxxxx这样的,至少差2位。。
4:如果还不放心随机个N组来搞搞。。
5:最好用long long免得溢出。。
6:没有了。。

对拍器:

#include <vector>
#include <algorithm>
#include <utility>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <sstream>
#define rep(i,n) for(int i=0;i<n;i++)
#define pb push_back
const int inf=~0U>>1;
using namespace std;
typedef ostringstream OSS;
int C[10];
bool Check(int a)
{
    OSS t;
    t<<a;
    string s=t.str();
    rep(i,s.size()-1)if(abs(s[i]-s[i+1])<2)return false;
    return true;
}
int main()
{
    int a,b,c=0;
    cin>>a>>b;
    for(int i=a; i<=b; i++)c+=Check(i);
    cout<<c<<endl;
}

代码:

#include <vector>
#include <algorithm>
#include <utility>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#define rep(i,n) for(int i=0;i<n;i++)
#define pb push_back
const int inf=~0U>>1,maxd=12;
using namespace std;
typedef long long ll;
ll Count[maxd][10]= {},C;
int N[maxd],n;
void PreDo()
{
    rep(j,10)Count[1][j]=1;
    for(int i=2; i<maxd; i++)
    {
        rep(j,10)
        {
            Count[i][j]=0;
            rep(k,10)if(abs(k-j)>=2)Count[i][j]+=Count[i-1][k];
        }
    }
}
void Dfs(int p,int l)
{
    int t=N[p];
    if(p==0)
    {
        rep(j,t+1)if(abs(j-l)>=2)C++;
        return;
    }
    rep(i,t)
    {
        if(abs(i-l)>=2)
        {
            if(p==n-1&&i==0)
            {
                rep(j,p)rep(k,9)C+=Count[j+1][k+1];
            }
            else
            {
                C+=Count[p+1][i];
            }
        }
    }
    if(abs(t-l)>=2)Dfs(p-1,t);
}
ll Cal(ll A)
{
    if(!A)return 0;
    for(n=0; A; A/=10,n++)N[n]=A%10;
    C=0;
    Dfs(n-1,20);
    return C;
}
int main()
{
//freopen("in","r",stdin);
    PreDo();
    ll a,b;
    cin>>a>>b;
    cout<<Cal(b)-Cal(a-1)<<endl;
}

One thought on “[SCOI2009]windy数

Leave a Reply to fan_hd Cancel reply

Your email address will not be published. Required fields are marked *