2015 HIAST Collegiate Programming Contest H
A sequence of positive and non-zero integers called palindromic if it can be read the same forward and backward, for example:
15 2 6 4 6 2 15
20 3 1 1 3 20
We have a special kind of palindromic sequences, let's call it a special palindrome.
A palindromic sequence is a special palindrome if its values don't decrease up to the middle value, and of course they don't increase from the middle to the end.
The sequences above is NOT special, while the following sequences are:
1 2 3 3 7 8 7 3 3 2 1
2 10 2
1 4 13 13 4 1
Let's define the function F(N), which represents the number of special sequences that the sum of their values is N.
For example F(7) = 5 which are : (7), (1 5 1), (2 3 2), (1 1 3 1 1), (1 1 1 1 1 1 1)
Your job is to write a program that compute the Value F(N) for given N's.
Input
The Input consists of a sequence of lines, each line contains a positive none zero integer N less than or equal to 250. The last line contains 0 which indicates the end of the input.
Output
Print one line for each given number N, which it the value F(N).
Example
1
3
7
10
0
2
5
17
解题思路:
要求一个数能分解成的回文串的数量,首先他要想成为回文串,左右两边必须相等,奇偶性讨论:
1.当n为奇数时,有中间数字,且必为奇数,因为两边相等加起来必为偶数,那么中间的数字必为小于等于n的奇数,遍历中间数,对其中一边进行整数划分
2.当n为偶数是,有两种情况:
(1).n所化成的回文串长度为奇数,那么中间数必为偶数,直接按上面遍历划分就行,
(2).回文串长度为偶数时,直接对n/2进行整数划分。两种情况的值加起来就是n为偶数是能分解成的回文串数量
注意:整数划分最好不要用递归法,效率太低了,很容易超时。用dp打表还是比较稳的。而且数值比较大最好用long long。
实现代码:
#include<bits/stdc++.h>
using namespace std;
#define Max 255
#define ll long long
ll f[Max][Max];
void fun(){
for(int i=;i<;i++){
for(int j=;j<;j++){
if(i==||j==) f[i][j] = ;
else if(i==||j==) f[i][j] = ;
else if(i<j) f[i][j] = f[i][i];
else if(i==j) f[i][j] = f[i][j-] + ;
else f[i][j] = f[i-j][j] + f[i][j-];
}
}
}
int main()
{
ll ans,n,i;
fun();
//cout<<f[250][250]<<endl;
while(cin>>n&&n){
ans = ;
if(n%==){
for(i=;i<=n;i+=){
ans+=f[(n-i)/][i];
}
}
else{
for(i=;i<=n;i+=){
ans+=f[(n-i)/][i];
}
ans+=f[n/][n/];
}
cout<<ans<<endl;
}
return ;
}
2015 HIAST Collegiate Programming Contest H的更多相关文章
- Gym 100952H&&2015 HIAST Collegiate Programming Contest H. Special Palindrome【dp预处理+矩阵快速幂/打表解法】
H. Special Palindrome time limit per test:1 second memory limit per test:64 megabytes input:standard ...
- Gym 100952E&&2015 HIAST Collegiate Programming Contest E. Arrange Teams【DFS+剪枝】
E. Arrange Teams time limit per test:2 seconds memory limit per test:64 megabytes input:standard inp ...
- Gym 100952F&&2015 HIAST Collegiate Programming Contest F. Contestants Ranking【BFS+STL乱搞(map+vector)+优先队列】
F. Contestants Ranking time limit per test:1 second memory limit per test:24 megabytes input:standar ...
- The 2015 China Collegiate Programming Contest H. Sudoku hdu 5547
Sudoku Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Subm ...
- Gym 100952J&&2015 HIAST Collegiate Programming Contest J. Polygons Intersection【计算几何求解两个凸多边形的相交面积板子题】
J. Polygons Intersection time limit per test:2 seconds memory limit per test:64 megabytes input:stan ...
- Gym 100952I&&2015 HIAST Collegiate Programming Contest I. Mancala【模拟】
I. Mancala time limit per test:3 seconds memory limit per test:256 megabytes input:standard input ou ...
- Gym 100952G&&2015 HIAST Collegiate Programming Contest G. The jar of divisors【简单博弈】
G. The jar of divisors time limit per test:2 seconds memory limit per test:64 megabytes input:standa ...
- Gym 100952D&&2015 HIAST Collegiate Programming Contest D. Time to go back【杨辉三角预处理,组合数,dp】
D. Time to go back time limit per test:1 second memory limit per test:256 megabytes input:standard i ...
- Gym 100952C&&2015 HIAST Collegiate Programming Contest C. Palindrome Again !!【字符串,模拟】
C. Palindrome Again !! time limit per test:1 second memory limit per test:64 megabytes input:standar ...
随机推荐
- linux调度器源码分析 - 初始化(二)
本文为原创,转载请注明:http://www.cnblogs.com/tolimit/ 引言 上期文章linux调度器源码分析 - 概述(一)已经把调度器相关的数据结构介绍了一遍,本篇着重通过代码说明 ...
- C#中的位的或运算的理解
如果懂位的运算,看到下面这2个程序执行的结果,会很容易理解,如果像我这样的菜鸟,刚接触开始肯定也觉得晕晕的,|= 这是什么运算符? |=就是位的或运算符,下面还是用上面的程序来讲解一下,为什么上面2个 ...
- BZOJ4237 JOISC2014 稻草人 CDQ分治、单调栈
传送门 题意:给出平面上$N$个点,求满足以下两个条件的矩形:①左下角与右上角各有一个点:②矩形内部没有点.$N \leq 2 \times 10^5$,所有数字大于等于$0$,保证坐标两两不同 最开 ...
- RDLC报表显示存储于数据库的图片
图片以二进制存储于数据库表中.在显示RDLC报表时,把图片呈现出来. 好吧. 把存储过程写好: CREATE PROCEDURE [dbo].[usp_File_Select] AS SELECT [ ...
- WPF-利用Blend写的平面控制闸门开关动画
原文:WPF-利用Blend写的平面控制闸门开关动画 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/yangyisen0713/article/de ...
- Quartz.Net分布式任务管理平台(续)
感谢@Taking园友得建议,我这边确实多做了一步上传,导致后面还需处理同步上传到其他服务器来支持分布式得操作.所有才有了上篇文章得完善. 首先看一下新的项目结构图: 这个图和上篇文章中 ...
- SpringMvc返回Json调试
spring-web-5.0.6.RELEASE.jar!/org/springframework/web/method/support/HandlerMethodReturnValueHandler ...
- 测试网站页面网速的一个简单Python脚本
无聊之余,下面分享一个Python小脚本:测试网站页面访问速度 [root@huanqiu ~]# vim pywww.py #!/usr/bin/python # coding: UTF-8 imp ...
- C_数据结构_递归实现累加
# include <stdio.h> long sum(int n) { //用递归实现: ) ; else ) + n; /* 用for循环实现: long s = 0; int i; ...
- Codeforces Round #504 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final)-D- Array Restoration
我们知道不满足的肯定是两边大中间小的,这样就用RMQ查询两个相同等值的区间内部最小值即可,注意边界条件 #include<bits/stdc++.h> #define x first #d ...