P2657 [SCOI2009]windy数

题目描述

windy定义了一种windy数。不含前导零且相邻两个数字之差至少为2的正整数被称为windy数。 windy想知道,

在A和B之间,包括A和B,总共有多少个windy数?

输入输出格式

输入格式:

包含两个整数,A B。

输出格式:

一个整数

输入输出样例

输入样例#1:

1 10

输出样例#1:

9

输入样例#2:

25 50

输出样例#2:

20

说明

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

Solution:

  本题显然数位$DP$,暂时不会(留着填坑~)。

  提供打表的思路,先线下每$10^6$个处理一次,统计出$2000$个答案(前缀和$sum[i]$表示$1$到$i*10^6$中满足条件的个数)。

  那么查询时就直接瞎搞模拟,最多计算$10^6$次。

打表代码:

 1 /**************************************************************
 2     Problem: 1026
 3     User: five20
 4     Language: C++
 5     Result: Accepted
 6     Time:256 ms
 7     Memory:1296 kb
 8 ****************************************************************/
 9
10 #include<iostream>
11 #include<cstdio>
12 #include<algorithm>
13 #include<cmath>
14 #define For(i,a,b) for(int (i)=(a);(i)<=(b);(i)++)
15 using namespace std;
16 const int N=1e6+5;
17 int ans,a,b,sum[2005]={0, 202174, 136131, 138503, 138214, 138252, 138252, 138214, 138503, 136131, 155315, 0, 0, 0, 138214, 138252, 138252, 138214, 138503, 136131, 155315, 155315, 0, 0, 0, 138252, 138252, 138214, 138503, 136131, 155315, 155315, 136131, 0, 0, 0, 138252, 138214, 138503, 136131, 155315, 155315, 136131, 138503, 0, 0, 0, 138214, 138503, 136131, 155315, 155315, 136131, 138503, 138214, 0, 0, 0, 138503, 136131, 155315, 155315, 136131, 138503, 138214, 138252, 0, 0, 0, 136131, 155315, 155315, 136131, 138503, 138214, 138252, 138252, 0, 0, 0, 155315, 155315, 136131, 138503, 138214, 138252, 138252, 138214, 0, 0, 0, 155315, 136131, 138503, 138214, 138252, 138252, 138214, 138503, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 155315, 136131, 0, 0, 0, 138252, 138214, 138503, 136131, 155315, 155315, 136131, 138503, 0, 0, 0, 138214, 138503, 136131, 155315, 155315, 136131, 138503, 138214, 0, 0, 0, 138503, 136131, 155315, 155315, 136131, 138503, 138214, 138252, 0, 0, 0, 136131, 155315, 155315, 136131, 138503, 138214, 138252, 138252, 0, 0, 0, 155315, 155315, 136131, 138503, 138214, 138252, 138252, 138214, 0, 0, 0, 155315, 136131, 138503, 138214, 138252, 138252, 138214, 138503, 0, 0, 0, 0, 138503, 138214, 138252, 138252, 138214, 138503, 136131, 155315, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 155315, 136131, 138503, 0, 0, 0, 138214, 138503, 136131, 155315, 155315, 136131, 138503, 138214, 0, 0, 0, 138503, 136131, 155315, 155315, 136131, 138503, 138214, 138252, 0, 0, 0, 136131, 155315, 155315, 136131, 138503, 138214, 138252, 138252, 0, 0, 0, 155315, 155315, 136131, 138503, 138214, 138252, 138252, 138214, 0, 0, 0, 155315, 136131, 138503, 138214, 138252, 138252, 138214, 138503, 0, 0, 0, 0, 138503, 138214, 138252, 138252, 138214, 138503, 136131, 155315, 0, 0, 0, 138214, 138252, 138252, 138214, 138503, 136131, 155315, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 155315, 136131, 138503, 138214, 0, 0, 0, 138503, 136131, 155315, 155315, 136131, 138503, 138214, 138252, 0, 0, 0, 136131, 155315, 155315, 136131, 138503, 138214, 138252, 138252, 0, 0, 0, 155315, 155315, 136131, 138503, 138214, 138252, 138252, 138214, 0, 0, 0, 155315, 136131, 138503, 138214, 138252, 138252, 138214, 138503, 0, 0, 0, 0, 138503, 138214, 138252, 138252, 138214, 138503, 136131, 155315, 0, 0, 0, 138214, 138252, 138252, 138214, 138503, 136131, 155315, 155315, 0, 0, 0, 138252, 138252, 138214, 138503, 136131, 155315, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 155315, 136131, 138503, 138214, 138252, 0, 0, 0, 136131, 155315, 155315, 136131, 138503, 138214, 138252, 138252, 0, 0, 0, 155315, 155315, 136131, 138503, 138214, 138252, 138252, 138214, 0, 0, 0, 155315, 136131, 138503, 138214, 138252, 138252, 138214, 138503, 0, 0, 0, 0, 138503, 138214, 138252, 138252, 138214, 138503, 136131, 155315, 0, 0, 0, 138214, 138252, 138252, 138214, 138503, 136131, 155315, 155315, 0, 0, 0, 138252, 138252, 138214, 138503, 136131, 155315, 155315, 136131, 0, 0, 0, 138252, 138214, 138503, 136131, 155315, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 155315, 136131, 138503, 138214, 138252, 138252, 0, 0, 0, 155315, 155315, 136131, 138503, 138214, 138252, 138252, 138214, 0, 0, 0, 155315, 136131, 138503, 138214, 138252, 138252, 138214, 138503, 0, 0, 0, 0, 138503, 138214, 138252, 138252, 138214, 138503, 136131, 155315, 0, 0, 0, 138214, 138252, 138252, 138214, 138503, 136131, 155315, 155315, 0, 0, 0, 138252, 138252, 138214, 138503, 136131, 155315, 155315, 136131, 0, 0, 0, 138252, 138214, 138503, 136131, 155315, 155315, 136131, 138503, 0, 0, 0, 138214, 138503, 136131, 155315, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 155315, 136131, 138503, 138214, 138252, 138252, 138214, 0, 0, 0, 155315, 136131, 138503, 138214, 138252, 138252, 138214, 138503, 0, 0, 0, 0, 138503, 138214, 138252, 138252, 138214, 138503, 136131, 155315, 0, 0, 0, 138214, 138252, 138252, 138214, 138503, 136131, 155315, 155315, 0, 0, 0, 138252, 138252, 138214, 138503, 136131, 155315, 155315, 136131, 0, 0, 0, 138252, 138214, 138503, 136131, 155315, 155315, 136131, 138503, 0, 0, 0, 138214, 138503, 136131, 155315, 155315, 136131, 138503, 138214, 0, 0, 0, 138503, 136131, 155315, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 155315, 136131, 138503, 138214, 138252, 138252, 138214, 138503, 0, 0, 0, 0, 138503, 138214, 138252, 138252, 138214, 138503, 136131, 155315, 0, 0, 0, 138214, 138252, 138252, 138214, 138503, 136131, 155315, 155315, 0, 0, 0, 138252, 138252, 138214, 138503, 136131, 155315, 155315, 136131, 0, 0, 0, 138252, 138214, 138503, 136131, 155315, 155315, 136131, 138503, 0, 0, 0, 138214, 138503, 136131, 155315, 155315, 136131, 138503, 138214, 0, 0, 0, 138503, 136131, 155315, 155315, 136131, 138503, 138214, 138252, 0, 0, 0, 136131, 155315, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 138503, 138214, 138252, 138252, 138214, 138503, 136131, 155315, 0, 0, 0, 138214, 138252, 138252, 138214, 138503, 136131, 155315, 155315, 0, 0, 0, 138252, 138252, 138214, 138503, 136131, 155315, 155315, 136131, 0, 0, 0, 138252, 138214, 138503, 136131, 155315, 155315, 136131, 138503, 0, 0, 0, 138214, 138503, 136131, 155315, 155315, 136131, 138503, 138214, 0, 0, 0, 138503, 136131, 155315, 155315, 136131, 138503, 138214, 138252, 0, 0, 0, 136131, 155315, 155315, 136131, 138503, 138214, 138252, 138252, 0, 0, 0, 155315, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,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0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 155315, 136131, 138503, 138214, 0, 0, 0, 138503, 136131, 155315, 155315, 136131, 138503, 138214, 138252, 0, 0, 0, 136131, 155315, 155315, 136131, 138503, 138214, 138252, 138252, 0, 0, 0, 155315, 155315, 136131, 138503, 138214, 138252, 138252, 138214, 0, 0, 0, 155315, 136131, 138503, 138214, 138252, 138252, 138214, 138503, 0, 0, 0, 0, 138503, 138214, 138252, 138252, 138214, 138503, 136131, 155315, 0, 0, 0, 138214, 138252, 138252, 138214, 138503, 136131, 155315, 155315, 0, 0, 0, 138252, 138252, 138214, 138503, 136131, 155315, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 155315, 136131, 138503, 138214, 138252, 0, 0, 0, 136131, 155315, 155315, 136131, 138503, 138214, 138252, 138252, 0, 0, 0, 155315, 155315, 136131, 138503, 138214, 138252, 138252, 138214, 0, 0, 0, 155315, 136131, 138503, 138214, 138252, 138252, 138214, 138503, 0, 0, 0, 0, 138503, 138214, 138252, 138252, 138214, 138503, 136131, 155315, 0, 0, 0, 138214, 138252, 138252, 138214, 138503, 136131, 155315, 155315, 0, 0, 0, 138252, 138252, 138214, 138503, 136131, 155315, 155315, 136131, 0, 0, 0, 138252, 138214, 138503, 136131, 155315, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 155315, 136131, 138503, 138214, 138252, 138252, 0, 0, 0, 155315, 155315, 136131, 138503, 138214, 138252, 138252, 138214, 0, 0, 0, 155315, 136131, 138503, 138214, 138252, 138252, 138214, 138503, 0, 0, 0, 0, 138503, 138214, 138252, 138252, 138214, 138503, 136131, 155315, 0, 0, 0, 138214, 138252, 138252, 138214, 138503, 136131, 155315, 155315, 0, 0, 0, 138252, 138252, 138214, 138503, 136131, 155315, 155315, 136131, 0, 0, 0, 138252, 138214, 138503, 136131, 155315, 155315, 136131, 138503, 0, 0, 0, 138214, 138503, 136131, 155315, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 155315, 136131, 138503, 138214, 138252, 138252, 138214, 0, 0, 0, 155315, 136131, 138503, 138214, 138252, 138252, 138214, 138503, 0, 0, 0, 0, 138503, 138214, 138252, 138252, 138214, 138503, 136131, 155315, 0, 0, 0, 138214, 138252, 138252, 138214, 138503, 136131, 155315, 155315, 0, 0, 0, 138252, 138252, 138214, 138503, 136131, 155315, 155315, 136131, 0, 0, 0, 138252, 138214, 138503, 136131, 155315, 155315, 136131, 138503, 0, 0, 0, 138214, 138503, 136131, 155315, 155315, 136131, 138503, 138214, 0, 0, 0, 138503, 136131, 155315, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 155315, 136131, 138503, 138214, 138252, 138252, 138214, 138503, 0, 0, 0, 0, 138503, 138214, 138252, 138252, 138214, 138503, 136131, 155315, 0, 0, 0, 138214, 138252, 138252, 138214, 138503, 136131, 155315, 155315, 0, 0, 0, 138252, 138252, 138214, 138503, 136131, 155315, 155315, 136131, 0, 0, 0, 138252, 138214, 138503, 136131, 155315, 155315, 136131, 138503, 0, 0, 0, 138214, 138503, 136131, 155315, 155315, 136131, 138503, 138214, 0, 0, 0, 138503, 136131, 155315, 155315, 136131, 138503, 138214, 138252, 0, 0, 0, 136131, 155315, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 138503, 138214, 138252, 138252, 138214, 138503, 136131, 155315, 0, 0, 0, 138214, 138252, 138252, 138214, 138503, 136131, 155315, 155315, 0, 0, 0, 138252, 138252, 138214, 138503, 136131, 155315, 155315, 136131, 0, 0, 0, 138252, 138214, 138503, 136131, 155315, 155315, 136131, 138503, 0, 0, 0, 138214, 138503, 136131, 155315, 155315, 136131, 138503, 138214, 0, 0, 0, 138503, 136131, 155315, 155315, 136131, 138503, 138214, 138252, 0, 0, 0, 136131, 155315, 155315, 136131, 138503, 138214, 138252, 138252, 0, 0, 0, 155315, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
18 inline void check(int x){
19     if(x<=9){ans++;return;}
20     int a=x,b=-5;
21     while(a){
22         if(abs(a%10-b)<2)return;
23         b=a%10;a=a/10;
24     }
25     ans++;
26 }
27 int main(){
28     cin>>a>>b;
29     if(b-a<=1000000){
30         For(i,a,b)check(i);
31         cout<<ans;
32         return 0;
33     }
34     int p=ceil(a*1.0/1000000),q=floor(b*1.0/1000000);
35     For(i,1,2000)sum[i]+=sum[i-1];
36     ans+=sum[q]-sum[p];
37     if(!ans)ans++;
38     p=p*1000000,q=q*1000000;
39     if(a!=p)
40     For(i,a,p-1)check(i);
41     if(b!=q)
42     For(i,q+1,b)check(i);
43     cout<<ans;
44     return 0;
45 }

 

原文地址:https://www.cnblogs.com/five20/p/9040490.html

时间: 2024-10-19 06:38:25

P2657 [SCOI2009]windy数的相关文章

luogu P2657 [SCOI2009]windy数 数位dp 记忆化搜索

题目链接 luogu P2657 [SCOI2009]windy数 题解 我有了一种所有数位dp都能用记忆话搜索水的错觉 代码 #include<cstdio> #include<algorithm> inline int read() { int x = 0,f = 1; char c = getchar(); while(c < '0' || c > '9') c = getchar(); while(c <= '9' && c >= '

Luogu P2657 [SCOI2009]windy数

题目 首先我们可以非常轻松地预处理出\(f_{i,j}\)表示一个最高位为\(i\)位且该位为\(j\)的windy数的个数. 然后我们可以利用经典容斥把答案变成求\([1,x]\)的windy数个数. 设\(x\)有\(len\)位,从低到高位分别是\(a_1,\cdots,a_{len}\) 首先我们把位数小于\(len\)的答案求出. 然后求出位数等于\(len\)且首位小于\(a_{len}\)的答案. 然后我们从大到小枚举\(len\sim i-1\)位相等,\(i\)位不等,枚举第\

$P2657\ [SCOI2009]\ windy$数

属于数位\(DP\)入门级别的题目,但我做这类题不多,还是要总结一下这道经典题目 \(Description\) 题面 给定\(a,b\),求\([a,b]\)区间有多少个数满足:任意两个相邻数位之间的差的绝对值\(>=2\) \(a,b<=1e12\) \(Solution\) 数位\(DP\)的基本思想是一个一个数确定,逼近到边界 数位\(DP\)一般设计状态为\(dp[i][s]\)表示当前考虑到第\(i\)位(从最低位编号),当前位置或附近位置状态为\(s\)的方案数. 有时候需要预处

P2657 [SCOI2009]windy数 (数位DP)

题目地址  注意点: 边界讨论. #include<cstdio> #include<iostream> #include<cstring> #include<algorithm> using namespace std; const int INF=2e9; int dp[15][15]; int maxNum[15];//每一位数字的最大值 int dfs(int len,int lastVal,bool isMaxed,bool isLead){//l

【luogu2657】【bzoj1026】 [SCOI2009]windy数 [动态规划 数位dp]

P2657 [SCOI2009]windy数 bzoj1026 一本通说这是一道数位dp模板题 emmmmm 就是逐位确定 f[i][j]表示填了i位数其最高位数字为j 然后就去求可能方案数 分为 不满足x的位数的严格小于x的全部情况 和x的位数相同 但最高位小于x的最高为的全部方案数 和x的位数相同 有一位比x的对应位数小的全部方案数 其余位数对应数字都相同(这是数位dp常用的一个性质:对于一个小于n的数 它从高位到低位一定会出现某一位上的数字小于n所对应这一位上的数字) PS 因为x不一定为

bzoj1026 [SCOI2009]windy数

1026: [SCOI2009]windy数 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 6392  Solved: 2854[Submit][Status][Discuss] Description windy定义了一种windy数.不含前导零且相邻两个数字之差至少为2的正整数被称为windy数. windy想知道,在A和B之间,包括A和B,总共有多少个windy数? Input 包含两个整数,A B. Output 一个整数 Sample I

bzoj 1026 [SCOI2009]windy数(数位DP)

1026: [SCOI2009]windy数 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 4550  Solved: 2039[Submit][Status][Discuss] Description windy定义了一种windy数.不含前导零且相邻两个数字之差至少为2的正整数被称为windy数. windy想知道,在A和B之间,包括A和B,总共有多少个windy数? Input 包含两个整数,A B. Output 一个整数. Sample

bzoj1026: [SCOI2009]windy数(数位dp)

1026: [SCOI2009]windy数 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 8203  Solved: 3687[Submit][Status][Discuss] Description windy定义了一种windy数.不含前导零且相邻两个数字之差至少为2的正整数被称为windy数. windy想知道, 在A和B之间,包括A和B,总共有多少个windy数? Input 包含两个整数,A B. Output 一个整数 Sample

bzoj 1026 [SCOI2009]windy数 数位dp

1026: [SCOI2009]windy数 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/problem.php?id=1026 Description windy定义了一种windy数.不含前导零且相邻两个数字之差至少为2的正整数被称为windy数. windy想知道,在A和B之间,包括A和B,总共有多少个windy数? Input 包含两个整数,A B. Output 一个整数.