HDU-1028-Ignatius and the Princess III

Source: Internet
Author: User
Tags integer division

HDU-1028-Ignatius and the Princess III

Http://acm.hdu.edu.cn/showproblem.php? Pid = 1, 1028

Integer Division, helpless, timeout

# Include <stdio. h> int huafen (int n, int m) // divide n into the maximum number of integers not greater than m {if (n <1 | m <1) return 0; if (n = 1 | m = 1) return 1; if (n <m) return huafen (n, n); if (n = m) return huafen (n m-1) + 1; return huafen (m-1) + huafen (n-m, m ); // The maximum addition = m and the maximum addition <= composition of the analysis result of the analysis.} int main () {int t; while (scanf ("% d", & t )! = EOF) printf ("% d \ n", huafen (t, t); return 0 ;}

Referring to TankyWoo's blog, I used the primary function to solve the problem of integer division.

Example

Each weight has one weight of one gram, two grams, three grams, and four grams. What kind of weight can be claimed? What are the possible solutions for each weight?

One 1 gram weight can be represented by the function 1 + x

1 2 grams of weight can be expressed by the function 1 + x ^ 2

1 3 grams of weight can be represented by the function 1 + x ^ 3

1 4 grams of weight can be represented by the function 1 + x ^ 4

(1 + x) (1 + x ^ 2) (1 + x ^ 3) (1 + x ^ 4)

= (1 + x ^ 2 + x ^ 3) (1 + x ^ 3 + x ^ 4 + x ^ 7)

= 1 + x ^ 2 + 2x ^ 3 + 2x ^ 4 + 2x ^ 5 + 2x ^ 6 + 2x ^ 7 + x ^ 8 + x ^ 9 + x ^ 10

For example, there are 2x ^ 5 items on the right end, that is, the 5 grams of the solution are 2: 5 = 3 + 2 = 4 + 1; 6 = 1 + 2 + 3 = 4 + 2; 10 = 1 + 2 + 3 + 4.

Therefore, there are 2 solutions for 6 grams and 1 solutions for 10 grams.

Thus, we can construct Integer Division functions.

G (x) = (1 + x ^ 2 + ......) * (1 + x ^ 2 + x ^ 4 .....) * (1 + x ^ 3 + x ^ 6 .......) *.......

Just multiply the polynomial to obtain the coefficient of x ^ n.

#include<stdio.h>int c1[10001],c2[10001];int main(){int n,i,j,k;while(scanf("%d",&n)!=EOF){for(i=0;i<=n;i++){c1[i]=1;c2[i]=0;}for(i=2;i<=n;i++){for(j=0;j<=n;j++)for(k=0;k+j<=n;k+=i)c2[j+k]+=c1[j];for(j=0;j<=n;j++){c1[j]=c2[j];c2[j]=0;}}printf("%d\n",c1[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.