zoukankan      html  css  js  c++  java
  • [CQOI2016] *

    Description

    ([l,r]) 中至少有 (3) 个连续相同数字,不同时有 (4,8) 的数字个数。

    Solution

    正常数位 dp 即可,注意前导零特判

    #include <bits/stdc++.h>
    using namespace std;
    
    #define int long long 
    const int N = 1000005;
    
    vector <int> a;
    
    int f[20][5][10][4];
    
    int solve(int pos,int con,int last,int flag,int full)
    {
        if(pos<0) return con==3;
        if(!full && ~f[pos][con][last][flag]) return f[pos][con][last][flag];
    
        int lim=full?a[pos]:9;
        int ans=0;
        
        for(int now=(con==0?1:0);now<=lim;now++)
        {
            int newpos=pos-1;
            int newcon=(now==last?con:0)+1;
            if(con>=3) newcon=3;
            int newlast=now;
            int newflag=flag|(2*(now==8))|(now==4);
            if(newflag==3) continue;
            int newfull=full&&now==lim;
            ans+=solve(newpos,newcon,newlast,newflag,newfull);
        }
    
        if(!full) f[pos][con][last][flag]=ans;
        return ans;
    }
    
    int solve(int x)
    {
        a.clear();
        memset(f,-1,sizeof f);
        while(x) 
        {
            a.push_back(x%10);
            x/=10;
        }
        return solve(a.size()-1,0,0,0,1);
    }
    
    signed main()
    {
        ios::sync_with_stdio(false);
    
        int l,r;
        cin>>l>>r;
        --l;
        if(l>=(int)(1e10+0.5)) cout<<solve(r)-solve(l)<<endl;
        else cout<<solve(r)<<endl;
    
        return 0;
    }
    
  • 相关阅读:
    NAVICAT 拒绝链接的问题
    .net垃圾回收-原理浅析
    C#中标准Dispose模式的实现
    Windbg调试托管代码
    C#泛型基础
    .Net垃圾回收和大对象处理
    C++ 小知识点
    C++之虚函数表
    C++之指针与引用,函数和数组
    C++之const关键字
  • 原文地址:https://www.cnblogs.com/mollnn/p/14019864.html
Copyright © 2011-2022 走看看