POJ 1777 Vivian's Problem (Mason prime number)

Source: Internet
Author: User

Question: K count, p1, p2 ,...... Pk, not necessarily a prime number. Add an exponent to these numbers. The final product between 0 and 10 is n, and all its factors are m. Ask if there is a power where m is 2, if multiple outputs have the largest index, if NO output is available.
Http://poj.org/problem? Id = 1777
Here we need to use a special prime number: The Prime Number of Mason.
The prime number E that satisfies E = 2 ^ I-1 is called the prime number of Mason.
There is an important theorem about Mason prime numbers: "the product of a number that can be written into several non-repeated Mason prime numbers" is equivalent to "the approximate number of this number and the power of 2 ", but it cannot be repeated. For example, 3 is a prime number of Mason, and 9 does not satisfy the power of divisor 2.
Another important thing is that N's power and power can be directly summed up by the source power of the prime number of Mason.
The prime number of Mason is very small. In the range given by the question, there are only eight prime numbers of Mason. You can list them in a table and determine whether each given number has a unique prime number of Mason. A prime factor in Mason must have only one factor. As we have said before, it is not satisfied with 9.3*3, and it is certainly not satisfied with other factors.
Since there are only eight prime Mason numbers, you can use the state to compress the DP.
[Cpp]
# Include <iostream>
# Include <cstring>
# Include <queue>
# Include <cstdio>
# Include <cmath>
# Include <algorithm>
# Define N 30
# Define inf 1 <29
# Define MOD 2007.
# Define LL long
Using namespace std;
Int mason [8] = {3, 7, 31,127,819, 131071, 524287,2147483647 };
Int cnt [8] = {2, 3, 5, 7, 13, 17, 19,31 };
Int dp [1 <8];
Int change (int num ){
Int ret = 0;
For (int I = 0; I <8; I ++ ){
If (num % mason [I] = 0 ){
Num/= mason [I];
// This factor contains multiple and returns 0
If (num % mason [I] = 0) return 0;
Ret | = 1 <I;
}
}
// If there are other factors, return 0
If (num! = 1) return 0;
Return ret;
}
Int clac (int state ){
Int sum = 0;
For (int I = 0; I <8; I ++)
If (state & (1 <I ))
Sum + = cnt [I];
Return sum;
}
Int main (){
Int n, a [100];
While (scanf ("% d", & n )! = EOF ){
For (int I = 0; I <n; I ++ ){
Scanf ("% d", & a [I]);
A [I] = change (a [I]);
If (! A [I]) I --, n --;
}
// No factor meets the conditions
If (n = 0 ){
Puts ("NO ");
Continue;
}
Memset (dp, 0, sizeof (dp ));
Dp [0] = 1;
For (int I = 0; I <n; I ++ ){
For (int j = 0; j <(1 <8); j ++)
If (! (J & a [I])
Dp [j | a [I] | = dp [j];
}
Int ans = 0;
For (int I = 0; I <(1 <8); I ++)
If (dp [I]) www.2cto.com
Ans = max (ans, clac (I ));
Printf ("% d \ n", ans );
}
Return 0;
}
By ACM_cxlove

Related Article

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.