codeforces 55d (lcm+数位dp)附板子

版权声明:欢迎大佬指正! https://blog.csdn.net/sinat_36215255/article/details/81451540

引用一篇不错的博客好了。https://blog.csdn.net/qq_33184171/article/details/52332586

就是学习的人家的,很久不写,数位dp也忘记了咋写。

代码如下:

注意dp只需要初始化时更新一次就好,

dp[i][j][k] 表示,右数第 i 位 ,mod为 j 时,lcm为k 的符合要求数目, 此处把lcm离散化了一下,

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <string>
#include <map>
#include <vector>

using namespace std;
const int MOD = 2520;
long long dp[30][2525][50];
int num[20];
int has[3000];
int s[3000];
int gcd(int a,int b)
{
    if(!b) return a;
    else
        return gcd(b,a%b);
}
int getlcm(int a,int b)
{
     int d = gcd(a,b);
     return (a/d)*b;
}
long long dfs(int pos,int mod,int lcm,int limit)
{
    if(pos<0) return mod%lcm==0;
    if(!limit && dp[pos][mod][has[lcm]]!=-1)
        return dp[pos][mod][has[lcm]];
    int tlcm;
    int endi = 9;
    if(limit)
        endi = num[pos];
    long long res = 0;
    for(int i=0; i<=endi; i++)
    {
        if(!i) tlcm = lcm;
        else  tlcm = getlcm(i,lcm);
        res +=dfs(pos-1,(mod*10+i)%MOD,tlcm,limit&&(i==endi));
    }
    if(!limit) dp[pos][mod][has[lcm]] = res;
    return res;

}
long long solve(long long n)
{
    int cnt = 0;
    while(n>0)
    {
        num[cnt++] = n%10;
        n/=10;
    }
  
    return dfs(cnt-1,0,1,1);
}
void init()
{
    memset(has,0,sizeof(has));
    int l = 0;
    for(int i=0; i<(1<<9);i++)
    {
        int now = 1;
        for(int j=1;j<9;j++)
        {
            if(i&(1<<j))
            {
                now = getlcm(now,j+1);
            }
        }
        has[now] = 1;
    }
    for(int i=0; i<=MOD; i++)
    {
        if(has[i])
        {
            s[l]= i;
            has[i] = l++;
        }
    }
}
int main()
{
      memset(dp,-1,sizeof(dp));
   int t;
   cin>>t;
   init();
   while(t--)
   {
       long long l,r;
       cin>>l>>r;
       long long ans1 = solve(l-1);
       long long ans2 = solve(r);
       cout<<ans2-ans1<<endl;
      // cout<<getlcm(l,r)<<endl;
   }
    return 0;
}

再接一个hls的数位dp板子


typedef long long ll;
int a[20];
ll dp[20][state];//不同题目状态不同
ll dfs(int pos,/*state变量*/,bool lead/*前导零*/,bool limit/*数位上界变量*/)//不是每个题都要判断前导零
{
    //递归边界,既然是按位枚举,最低位是0,那么pos==-1说明这个数我枚举完了
    if(pos==-1) return 1; 
           /*这里一般返回1,表示你枚举的这个数是合法的,那么这里就需要你在枚举时必须每一位都要满足题目条件,
          也就是说当前枚举到pos位,一定要保证前面已经枚举的数位是合法的。不过具体题目不同或者写法不同的话不一定要返回1 */

    //第二个就是记忆化(在此前可能不同题目还能有一些剪枝)
    if(!limit && !lead && dp[pos][state]!=-1) return dp[pos][state];
    /*常规写法都是在没有限制的条件记忆化,这里与下面记录状态是对应,具体为什么是有条件的记忆化后面会讲*/

    int up=limit?a[pos]:9;//根据limit判断枚举的上界up;这个的例子前面用213讲过了
    ll ans=0;

    //开始计数
    for(int i=0;i<=up;i++)//枚举,然后把不同情况的个数加到ans就可以了
    {
        if() ...
        else if()...
        ans+=dfs(pos-1,/*状态转移*/,lead && i==0,limit && i==a[pos]) //最后两个变量传参都是这样写的
        /*这里还算比较灵活,不过做几个题就觉得这里也是套路了
        大概就是说,我当前数位枚举的数是i,然后根据题目的约束条件分类讨论
        去计算不同情况下的个数,还有要根据state变量来保证i的合法性,比如题目
        要求数位上不能有62连续出现,那么就是state就是要保存前一位pre,然后分类,
        前一位如果是6那么这意味就不能是2,这里一定要保存枚举的这个数是合法*/
    }
    //计算完,记录状态
    if(!limit && !lead) dp[pos][state]=ans;
    /*这里对应上面的记忆化,在一定条件下时记录,保证一致性,当然如果约束条件不需要考虑lead,这里就是lead就完全不用考虑了*/
    return ans;
}
ll solve(ll x)
{
    int pos=0;
    while(x)//把数位都分解出来
    {
        a[pos++]=x%10;//个人老是喜欢编号为[0,pos),看不惯的就按自己习惯来,反正注意数位边界就行
        x/=10;
    }
    return dfs(pos-1/*从最高位开始枚举*/,/*一系列状态 */,true,true);
   //刚开始最高位都是有限制并且有前导零的,显然比最高位还要高的一位视为0嘛
}
int main()
{
    ll le,ri;
    while(~scanf("%lld%lld",&le,&ri))
    {
        //初始化dp数组为-1,这里还有更加优美的优化,后面讲
        printf("%lld\n",solve(ri)-solve(le-1));
    }
}

猜你喜欢

转载自blog.csdn.net/sinat_36215255/article/details/81451540
今日推荐