poj 3790 Recursively Palindromic Partitions

递归回文分区算法
本文探讨了如何计算一个整数的所有递归回文分区数量,通过递归方式实现并提供了一个实例代码。
/*
摘抄自博客:
Recursively Palindromic Partitions Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 472 Accepted: 345 Description A partition of a positive integer N is a sequence of integers which sum to N, usually written with plus signs between the numbers of the partition. For example 15 = 1+2+3+4+5 = 1+2+1+7+1+2+1 A partition is palindromic if it reads the same forward and backward. The first partition in the example is not palindromic while the second is. If a partition containing m integers is palindromic, its left half is the first floor(m/2) integers and its right half is the last floor(m/2) integers (which must be the reverse of the left half. (floor(x) is the greatest integer less than or equal to x.) A partition is recursively palindromic if it is palindromic and its left half is recursively palindromic or empty. Note that every integer has at least two recursively palindromic partitions one consisting of all ones and a second consisting of the integer itself. The second example above is also recursively palindromic. For example, the recursively palindromic partitions of 7 are: 7, 1+5+1, 2+3+2, 1+1+3+1+1, 3+1+3, 1+1+1+1+1+1+1 Write a program which takes as input an integer N and outputs the number of recursively palindromic partitions of N. Input The first line of input contains a single integer N, (1 <= N <= 1000) which is the number of data sets that follow. Each data set consists of a single line of input containing a single positive integer for which the number of recursively palindromic partitions is to be found. Output For each data set, you should generate one line of output with the following values: The data set number as a decimal integer (start counting at one), a space and the number of recursively palindromic partitions of the input value. Sample Input 3 4 7 20 Sample Output 1 4 2 6 3 60 Source Greater New York Regional 2008 题意是:求每个数分割成递归回文的个数,即将n分割后,再将其左侧和右侧分割,依次..自身也算一种; 以7为列: 0+7+0 f[0]; 1+5+1 f[1]; 2+3+2 f[2]; 3+1+3 f[3]; f[7]=f[0]+f[1]+f[2]+f[3]; 每个数的递归回文个数都等于它一半之前所有回文个数之和;其中f[0]=1;f[1]=1; 递归即可;偶数和其之后的奇数回文个数是相等的;*/ #include<stdio.h> int f[1005]; int main() { int n,m,i,j; f[0]=1; f[1]=1; for(j=2; j<=1000; j++) { f[j]=0; for(i=0; i<=j/2; i++) f[j]+=f[i]; } scanf("%d",&n); for(i=1; i<=n; i++) { scanf("%d",&m); printf("%d %d\n",i,f[m]); } return 0; }

 

转载于:https://www.cnblogs.com/heqinghui/p/3186900.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值