The first question uses the same nature, which is the most basic nature in number theory, but it cannot be found by yourself during Question preparation. N * m = 11111..., n,
The answer obtained by multiplying one m by n is a number composed of 1. Ask the minimum number of 1. Can be converted to n * m = (k * 10 + 1), then (k * 10 + 1) % n = 0.
Of course, the first k is 1, so we keep increasing k = (10 * k + 1 ). It depends on the number of increases, that is, the number of 1. To avoid overflow, keep % n.
Because of the same nature, the answer after % n remains unchanged.
The second method uses the prime number embedding method. The principle of the prime number embedding method is to sieve out the multiples of prime numbers. Because it is from small cycles to large, if the current value is not screened out, it must be a prime number,
This judgment leads to the complexity not the square of n.
Poj 2551 code:
# Include <stdio. h>
Int main ()
{
Int nN;
While (scanf ("% d", & nN) = 1)
{
Int nCnt = 1;
Int nTemp = 1;
While (1)
{
If (nTemp % nN = 0) break;
Else nTemp = (nTemp * 10 + 1) % nN;
++ NCnt;
}
Printf ("% d \ n", nCnt );
}
Return 0;
}
Poj 2262 code:
# Include <stdio. h>
# Include <string. h>
# Include <math. h>
# Define MAX (1000000 + 10)
Bool bPrime [MAX];
Void InitPrime ()
{
Memset (bPrime, true, sizeof (bPrime ));
BPrime [0] = bPrime [1] = false;
For (int I = 2; I <= MAX; ++ I)
{
If (bPrime [I])
For (int j = 2 * I; j <= MAX; j + = I)
{
BPrime [j] = false;
}
}
}
Int main ()
{
Int nN;
InitPrime ();
While (scanf ("% d", & nN), nN)
{
Int I;
For (I = 2; I <nN; ++ I)
{
If (I % 2 & (nN-I) % 2 & bPrime [I] & bPrime [nN-I])
{
Printf ("% d = % d + % d \ n", nN, I, nN-I );
Break; www.2cto.com
}
}
If (I = nN)
{
Printf ("Goldbach's conjecture is wrong. \ n ");
}
}
Return 0;
}