Poj-1980 unit fraction partition **

Source: Internet
Author: User
/*
* I thought it would be difficult to prune this question. I didn't expect 1A .. 32 Ms
* The difficulty of this question is the processing of scores .. Do not use double .. Accuracy is hard to grasp ..
*
*/

# Include <cstdio>
# Include <cmath>
Using namespace STD;

Int p, q, A, N, TOT; // if the question is defined, TOT is the answer

// Compare P1/Q1 and P2/Q2
Int inline frac_cmp (INT P1, int Q1, int P2, int q2 ){
Return P1 * Q2-P2 * Q1;
}
/*
Int inline gcd (int A, int B ){
If (B = 0) return;
Return gcd (B, A % B );
}
*/
// P/Q-1/D the answer is stored in P/Q
Void inline frac_sub (Int & P, Int & Q, int d ){
P = p * D-Q;
Q = Q * D;

// Save time (600 MS-> 63 MS)
// If (P! = 0 ){
// Int G = gcd (p, q );
// P = P/g; q = Q/g;
//}
}

// The denominator of the last layer of last_d .. The numerator of the remaining left_p score .. The denominator of the remaining score of left_q .. Num layers .. Maximum Product of the Left denominator of left_a
Void DFS (INT last_d, int left_p, int left_q, int num, int left_a ){
Int next_p, next_q;
If (num = n + 1 & left_p! = 0) return;
If (Num <= n + 1 & left_p = 0 ){
TOT ++; return;
}

// If (frac_cmp (left_p, left_q, N-num + 1, last_d)> 0) return; // The remaining score is too large (which will be determined later, which can be omitted)

For (INT d = last_d; D <= left_a; D ++ ){
If (frac_cmp (left_p, left_q, 1, D) <0) continue; // 1/D is too large

Frac_sub (next_p = left_p, next_q = left_q, d); // obtain next_p, next_q
If (frac_cmp (next_p, next_q, N-num, d)> 0) break; // The score left for the next layer is too large

DFS (D, next_p, next_q, num + 1, left_a/D );
}

}


Int main (){
While (scanf ("% d", & P, & Q, & A, & N )){
If (P = 0) return 0;

TOT = 0;
DFS (1, p, q, 1, );
Printf ("% d \ n", TOT );

}

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.