题目链接
题目大意:找一个大于等于N的数,使这个数每一位只有0和1,且能被N整除。
Sample Input
2
6
19
0
Sample Output
10
100100100100100100
111111111111111111
数位dp记录不可行路径
#include <iostream>
#include <cstdio>
#include <ctime>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <cstdio>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <algorithm>
#define ll long long
#define IOS {ios::sync_with_stdio(0);cin.tie(0);}
using namespace std;
const int N = 1000;
int dp[300][300],n;
int flag;
bool dfs(int pos,int mod,int zero,string s){
if(pos==0) {
if(zero)return 0;
flag = ( mod==0);
if(flag==1) cout<<s<<endl; //访问到底层且合法一定是答案
return flag;
}
if(dp[pos][mod]!=-1) return dp[pos][mod]; //
int i;
for(i = 0;i <= 1;i ++){
string nxt=s;
if(!zero||(i==1)) nxt += char('0'+i);
dfs(pos-1,(mod*10%n+i)%n,zero&&i==0,nxt);
if(flag==1) return 1; //发现可行路径return
}
dp[pos][mod] = 0;
return 0; //该条路径不可行
}
int main(){
freopen("a.txt","r",stdin);
IOS;
while(1){
cin>>n;
if(n==0) break;
flag = 0;
memset(dp,-1,sizeof(dp));
dfs(100,0,1,"");
}
return 0;
}