Poj 2551 Ones and poj 2262 Goldbach's Conjecture

Source: Internet
Author: User

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;
}
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.