Enigma Gym - 101889E

http://codeforces.com/gym/101889/attachments

求给定字符串所能表示的n的最小倍数 n很小 字符串也很短 都是1e3 可以想到记忆化搜索 即搜到当前位置时高位的模数是多少 如果之前在这个位置并以这个模数走下去没结果 那这次也一样 标记下只走一次即可

#include <bits/stdc++.h>
using namespace std;
const int maxn=1e3+10;
const int maxm=1e3+10;

bool dp[maxn][maxm];
bool flag;
int n,m;
char ch[maxn];

void dfs(int cur,int val)
{
    int i;
    if(cur==n)
    {
        if(val==0)
        {
            printf("%s\n",ch);
            flag=1;
        }
        return;
    }
    if(ch[cur]=='?')
    {
        if(cur==0) i=1;
        else i=0;
        for(;i<10;i++)
        {
            if(!dp[cur][(10*val+i)%m])
            {
                ch[cur]='0'+i;
                dfs(cur+1,(10*val+i)%m);
                if(flag) return;
                ch[cur]='?';
                dp[cur][(10*val+i)%m]=1;
            }
        }
    }
    else
    {
        if(!dp[cur][(10*val+ch[cur]-'0')%m])
        {
            dfs(cur+1,(10*val+ch[cur]-'0')%m);
            if(flag) return;
            dp[cur][(10*val+ch[cur]-'0')%m]=1;
        }

    }
}

int main()
{
    scanf("%s%d",ch,&m);
    n=strlen(ch);
    dfs(0,0);
    if(!flag) printf("*\n");
    return 0;
}

猜你喜欢

转载自blog.csdn.net/sunyutian1998/article/details/83144475