Poj 1980 unit fraction Partition

Source: Internet
Author: User

I finally had a difficult question on my own (n_n)

It is said that it is a cut-off question, but my pruning is still to be improved. The following practices almost time limitted (985 ms)

 

My own ideas:

DFS search question, which is the total product of the current denominator, the number already exists, and the denominator value of the previous layer.

Returns false when the number already exists equal to A. Otherwise, the denominator value from the previous layer is the next score.

Check whether the conditions are met. If yes, add one.

 

# Include <iostream>

# Include <cmath>

Using namespace STD;

# Define x 1e-8

Int p, q, A, N;

Int ans;

Bool DFS (double CS, int ca, int CN, int CQ)

{// The remaining shard value, which is the total product of the current denominator. The number already exists. The denominator value of the previous layer.

If (CN = N)

Return false;

Int flag = 0;

For (Int J = CQ; j * Ca <= A; j ++)

{

If (FABS (cs-1.0/J) <X)

{// Because the current operation is successful, setting the flag indicates that the operation is successful, and the returned value is true.

Ans ++;

Flag = 1;

}

If (cs-1.0/j> 0 & DFS (cs-1.0/J, Ca * j, CN + 1, J ))

Flag = 1; // because the current operation is successful, setting the flag indicates that the operation is successful, and the returned value is true.

}

If (FLAG)

Return true;

Else

Return false;

}

Int main ()

{

Freopen ("sum. In", "r", stdin );

Freopen ("sum. Out", "W", stdout );

While (CIN> P> q> A> n, p | q | A | N)

{

Ans = 0;

DFS (p * 1.0/Q, 1, 0, 1 );

Cout <ans <Endl;

}

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.