HOJ 1008

一道水题

多一个n之后除m的余数为之前的10倍+n

加一个记忆化搜索

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
inline int read(){
	int x=0,f=1,ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
	return 0;
}
bool dp[10000];
int n,m;
int ans[10][10001];
int main(){
	while(scanf("%d %d",&n,&m)!=EOF){
		// cout<<ans[n%m][m]<<endl;
		if(ans[n%m][m]){
			printf("%d\n",ans[n%m][m]);
			continue;
		}
		memset(dp,0,m*2);
		int res=n%m;
		dp[res]=1;ans[n%m][m]=1;
		while(res!=0){
			ans[n%m][m]++;
			res*=10;
			res+=n;
			res%=m;
			if(dp[res]){
				ans[n%m][m]=0;
				break;
			}
			dp[res]=1;
		}
		printf("%d\n",ans[n%m][m]);

	}
	return 0;
}

  

posted @ 2018-08-30 17:34  古城独钓  阅读(257)  评论(0编辑  收藏  举报