首页 > 其他分享 >CodeForces - 204A Little Elephant and Interval

CodeForces - 204A Little Elephant and Interval

时间:2022-11-03 18:35:55浏览次数:62  
标签:fir Little ll pos CodeForces Elephant con dp define

题意:给定区间[l, r],问区间内有多少第一个数字和末尾数字一样的数。

解:练习一些数位dp。先从高到低dp[pos][fir]表示dp到第pos个位置,以fir开头的串的数量,其中fir不为0。这道题0055和55显然不同,有前导0的影响,因此再加一维[con],然后en往板子里套。

代码:

#include <bits/stdc++.h>
using namespace std;
#define maxx 100005
#define maxn 25
#define maxm 205
#define ll long long
#define inf 1000000009
#define mod 1000000007
int a[20];
int len;
ll dp[20][10][2]={0};
ll dfs(ll pos,ll fir,ll pre,ll con,ll limit){
    if(pos==0) {
        return !con&&pre==fir;
    }
    if(!limit&&fir!=-1&&dp[pos][fir][con]!=-1)
        return dp[pos][fir][con];
    ll ret=0;
    ll res=limit?a[pos]:9;
    for(int i=0;i<=res;i++){
        if(con&&(i!=0)) fir=i;
        ret+= dfs(pos-1,fir,i,con&&(i==0),limit&&(a[pos]==i));
    }
    return !limit?dp[pos][fir][con]=ret:ret;
}
ll solve(ll x){
    len=0;
    while(x){
        a[++len]=x%10;
        x/=10;
    }
    return dfs(len,-1,0,1,1);
}
signed main() {
//    int T;
//    scanf("%d",&T);
//    while(T--) {;
//
//    }
    ll l,r;
    memset(dp,-1,sizeof dp);
    cin>>l>>r;
    cout<<solve(r)- solve(l-1)<<'\n';
    return 0;
}
View Code

 

标签:fir,Little,ll,pos,CodeForces,Elephant,con,dp,define
From: https://www.cnblogs.com/capterlliar/p/16855427.html

相关文章

  • Codeforces Round #610 (Div. 2) C
    C.PetyaandExamhttps://codeforces.com/contest/1282/problem/C考虑贪心先对于时间排序然后贪心我们可以不考那我们可以在此之前离开然后在离开之前这段时间多做......
  • Codeforces Round #634 (Div. 3) E
    E2.ThreeBlocksPalindrome(hardversion)我们考虑一种最优构造对于一个数x我们肯定要的是他的前几个再最后几个中间选最多的一个数这样显然是最优的我们枚举x......
  • Codeforces Global Round 7 D
    D1.Prefix-SuffixPalindrome(Easyversion)easy版本我们只需要n2dp预处理出快速判断回文串然后我们再通过双指针O(n)求解最大串intdp[5010][5010];voidsolve(){......
  • Codeforces Round #820 (Div. 3) A-G
    比赛链接A题解知识点:模拟时间复杂度\(O(1)\)空间复杂度\(O(1)\)代码#include<bits/stdc++.h>#definelllonglongusingnamespacestd;boolsolve(){......
  • Codeforces Round #611 (Div. 3) E
    E.NewYearParties对于最大值我们显然可以sort之后贪心一下即可正确性显然对于最小值我们发现会有三种情况一种是三个挨在一起一种是两个挨在一起还有一种就是......
  • J - Just Arrange the Icons CodeForces - 1267J (思维+暴力)
    题意n个软件,每个软件都有种类,而屏幕有容量s(自己决定),有两个限制:一个屏幕只能放s-1或者s个软件一个屏幕上只能防同种的软件求最小的屏幕数。思路枚举s代......
  • 「题解」Codeforces 1322B Present
    看上去异或里面套了层加法不好拆位,但是依然可以对每个二进制位处理。现在考虑第\(k\)位,那么产生贡献的数字对一定满足以下条件之一:第\(k\)位相同且前\((k-1)\)位......
  • 「题解」Codeforces 930E Coins Exhibition
    看到题就先容斥。然后容斥系数太难算了就寄了,大概要分好几种情况讨论,于是就弃了。不容斥也能做。考虑限制将串划分成了若干段,然后一段一段dp.有没有什么好的方法描述这个......
  • Codeforces - 1391C - Cyclic Permutations(思维 + 组合数学 + 数论 + 图论、*1500)
    1391C-CyclicPermutations(⇔源地址)目录1391C-CyclicPermutations(⇔源地址)tag题意思路AC代码错误次数:0tag⇔思维、⇔组合数学、⇔数论、⇔......
  • Codeforces Round #611 (Div. 3) D
    D.ChristmasTrees显然对于一个中间的点要是不能向两边最近的扩展我们直接判定他没有用处了这样我们就有了bfs的做法我们先把原点放进去要是能扩展我们显然可以直接......