【(a+b)%d=(a%d+b%d)%d】poj 2551 Ones

简介:

网上有一个很好的方法,大概的思想就是

11...11(n个1)的余数=11..1(n-1个1)的余数*10+1。。。

发现聪明人还是多的,他们的详细思路是:

只用余数乘以10+1就可以了。。。
    如:3 满足题意能除以它的最小数111 , 答案是3
   分析:1%3*10+1=11  j = 1
        11%3*10+1=21  j =2
        21%3=0     j=3 
   相当于:1%3 
          11%3
          111%3


其实就是验证 (n%t*10+1)%t 的结果和 (n*10+1)%t 的结果是否一致,n不一定是111..1,经验证,真的是一样的,代码附后,在简化就是 (n%t*10)%t 和 (n*10)%t 的结果一样。。

经验证,这里的10可以为任何正整数。。。其实把n拆成两个数就可以理解了 x+y,x可以被t整除,y是余数

原理就是(a+b)%d=(a%d+b%d)%d


#include"stdio.h"

int main()
{
	int n;
	int i;
	int tmp;
	while(~scanf("%d",&n))
	{
		tmp=1;
		for(i=1; ;i++)
		{
			if(tmp%n==0)
				break;

			tmp=tmp%n*10+1;
		}

		printf("%d\n",i);
	}

	return 0;
}

验证代码:

#include"stdio.h"

int main()
{
	int n;
	int t;
	while(~scanf("%d%d",&n,&t))
	{
		printf("%d\n",(n%t*10+1)%t);
		printf("%d\n",(n*10+1)%t);
	}

	return 0;
}




相关文章
poj 3664
http://poj.org/problem?id=3664 进行两轮选举,第一轮选前n进入第二轮,第二轮选最高   #include #include using namespace std; struct vote { int a,b; int c; ...
728 0
|
人工智能 BI
poj-1006-Biorhythms
Description 人生来就有三个生理周期,分别为体力、感情和智力周期,它们的周期长度为23天、28天和33天。每一个周期中有一天是高峰。在高峰这天,人会在相应的方面表现出色。例如,智力周期的高峰,人会思维敏捷,精力容易高度集中。
610 0
|
人工智能
POJ 1936 All in All
Description You have devised a new encryption technique which encodes a message by inserting between its characters randomly generated strings in a clever way.
785 0
POJ 2262 Goldbach's Conjecture
Problem Description In 1742, Christian Goldbach, a German amateur mathematician, sent a letter to Leonhard Euler in which he made the foll...
1002 0
|
机器学习/深度学习
|
机器学习/深度学习
|
测试技术