how mang n

连山秀举 2009-05-09 08:45:17
How mang N
以下是这道题的描述(据传是道ACM):
Find a minimal interger K which is merely comprised of N and can be divided by M.

For example,11 is the minimal number that and be divided by 11, and it is comprised of two '1's, and 111111 can be divided by 13 which is comprised of six '1's.

Input
On each line of input , there will be two positive integer, N and M. N is a digit number, M is no more than 10000.

Output
On each single line, output the number of N, if no such K, output zero.

Sample Input
1 5
1 11
1 13


Sample Output
0
2
6
有高人给出如下答案,小子实在看不懂原理,请牛人指点,说明原理即可,谢谢!
#include <stdio.h>

int main( )
{
int n, m, t, f[ 10000 ], i, count;
while ( scanf("%d%d", &n, &m) == 2 )
{
count = t = 0;
for ( i = 0; i < m; i++ )
f[ i ] = 0;
while ( !f[ t ] )
{
count++;
f[ t ] = 1;
t = ( t * 10 + n ) % m;
}
if ( !t )
printf("%d\n", count);
else
printf("0\n");
}
return 0;
}
...全文
113 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
绿色夹克衫 2009-05-11
  • 打赏
  • 举报
回复
如果是我做,可能会先求一下输入两个数的最大公约数,然后将两个数约分为互质的两个数,剩下的就都可以转化为求1和后面的数的问题了
也许可以通过对后面的数进行因数分解,找到效率更高的构造法!
lvjaio5241 2009-05-10
  • 打赏
  • 举报
回复
看不懂,学习
Paradin 2009-05-09
  • 打赏
  • 举报
回复
t = (t*10+n)%m, 即每添个n检查余数,如果为0就找到了
f[t] 表示余数为t是否以出现,如果出现了,且不为0那么就是说不能被整除(开始循环)。
x%m 的值只可能是0,1,...m-1, 因而f要开10000大小

33,007

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧