Poj 2635 the embarrassed cryptographer (integer refresh, Qianjin manufacturing mode)

Source: Internet
Author: User
Tags modulus
The embarrassed cryptographer
Time limit:2000 ms   Memory limit:65536 K
Total submissions:9291   Accepted:2403

Description

The young and very promising cryptographer odd even has implemented the security module of a large system with thousands of users, which is now in use in his company. the cryptographic keys are created from the product of two primes, and are believed to be secure because there is no known method for factoring such a product has tively.
What odd even did not think of, was that both factors in a key shocould be large, not just their product. it is now possible that some of the users of the system have weak keys. in a desperate attempt not to be fired, Odd even secretly goes through all the users keys, to check if they are strong enough. he uses his very poweful Atari, and is especially careful when checking his boss' key.

Input

The input consists of no more than 20 test cases. each test case is a line with the integers 4 <= k <= 10100 and 2 <= L <= 106. K is the key itself, a product of two primes. l is the wanted minimum size of the factors in the key. the input set is terminated by a case where k = 0 and L = 0.

Output

For each number k, if one of its factors are strictly less than the required l, your program shoshould output "bad p", where p is the smallest factor in K. otherwise, it shoshould output "good ". cases shoshould be separated by a line-break.

Sample Input

 
143 10143 20667 20667 302573 302573 0

Sample output

 
Goodbad 11 goodbad 23 goodbad 31

Source

Nordic 2005

General question:

Given a large number k, k is the product value of two large prime numbers.

Then, specify the number l in an int.

Ask whether the smallest of the two large prime numbers is less than L. If it is smaller than l, the prime number is output.

 

Solution:

First, the illustrations of the question are speechless...

 

High Precision modulus + same modulus Theorem

 

1. Read K in char format. Convert K to the kilobytes of KT and the int type.

Converting numbers to a big hexadecimal system can accelerate the computing efficiency. If it is in decimal format, it takes a lot of time.

In decimal format.

For example, if k = 1234567890 is converted into a kilobytes, it becomes: KT = [1] [234] [567] [890].

For ease of processing, myProgramIs to store kt in "partial order, global Reverse Order" Mode

That is, KT = [890] [567] [234] [1] (a bracket represents an array element)

2. Create a table with prime numbers, and prefill all prime numbers in 10 ^ 6. When modulo is obtained, the enumerated values are less than l.

Note that the number of tables to be typed cannot exceed 100To ensure that the maximum prime number in the prime number table must be greater than 10 ^ 6. Otherwise, when l = W and K is good, the array will overhead and RE, this is because Prime is a negative infinite number after the boundary is exceeded, and the enumerated while (prime [pmin] <L) loop will fall into an endless loop.

3. High-Precision modulus.

It mainly uses the KT array and the same modulus theorem.

For example, to verify whether 123 is divisible by 3, only the model 124% 3 is required.

However, when 123 is a large number, it cannot be directly obtained. It can only be indirectly modulo the large number through the same modulus theorem.

The specific method is:

Calculate 1% 3 = 1 first

Calculate (1*10 + 2) % 3 = 0

Calculate (0*10 + 4) % 3 = 1

The result is 124% 3 = 1 indirectly. This is obviously correct.

It is not difficult to find that (1*10 + 2) * 10 + 4 = 124

This is the practice in a 10-digit system. The same is true for a thousand-digit system. You can change * 10 to * 1000.

 

 

AlgorithmIdea: The kilobytes of data indicates that the number is known, and the remainder can be obtained with high precision. However, we can say that the hundred-in-One TLE, the thousand-in-one AC, and the ten-in-one wa,

 

 

# Include <stdio. h>
# Include < String . H>
Const Int Maxn = 1000010 ;
Int Prime [maxn + 1 ];
Int Getprime ()
{
Memset (prime, 0 , Sizeof (PRIME ));
For ( Int I = 2 ; I <= maxn; I ++)
{
If (! Prime [I]) prime [++ prime [ 0 ] = I;
For ( Int J = 1 ; J <= prime [ 0 ] & Prime [J] <= maxn/I; j ++)
{
Prime [prime [J] * I] = 1 ;
If (I % prime [J] = 0 ) Break ;
}
}
Return Prime [ 0 ];
}

Int KT [ 100 ];
Int L;
Char STR [ 1000 ];

Bool MoD ( Int * K, Int P, Int Len)
{
Int Sq = 0 ;
For ( Int I = len- 1 ; I> = 0 ; I --)
Sq = (SQ * 1000 + K [I]) % P;
If (! SQ) Return False ;
Return True ;
}
Int Main ()
{
Getprime ();

While (Scanf ( " % S % d " , & STR, & L )! = EOF)
{
If (L = 0 & Strcmp (STR, " 0 " ) = 0 ) Break ;
Int Len = strlen (STR );
Memset (KT, 0 , Sizeof (KT ));
For (Int I = 0 ; I <Len; I ++)
{
Int II = (LEN + 2 -I )/ 3 - 1 ;
KT [II] = kt [II] * 10 + STR [I]- ' 0 ' ;
}
Int Lenkt = (LEN +2 )/ 3 ;
Bool Flag = True ;
Int Pmin = 1 ;
While (Prime [pmin] <L)
{
If (! MoD (KT, prime [pmin], lenkt ))
{
Flag = False ;
Printf (" Bad % d \ n " , Prime [pmin]);
Break ;
}
Pmin ++;
}
If (FLAG) printf ( " Good \ 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.