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 ...
随机推荐
- 一、java虚拟机内存区域
内存区域 java虚拟机在java程序的过程中会把它所管理的内存划分为若干个不同的数据区域.java虚拟机规范将JVM管理的内存分为:程序计数器.本地方法栈.Java虚拟机栈.方法区.Java堆.如下 ...
- [01] JSP的基本认识
1.什么是JSP JSP,全称JavaServer Pages,是由Sun Microsystems公司倡导和许多公司参与共同建立的一种使软件开发者可以响应客户端请求,而动态生成HTML.XML或其他 ...
- 如何在Virtual box 下安装Mac os
这几天,突然奇想,想要试一试Mac os ,毕竟是贵族系统,装完之后,确实感觉字体很不错. 其他更优秀的功能还没发现,不过,还是希望在这里做一个记录. 以下附录我参照的网址:https://blog ...
- Luogu P3177 [HAOI2015]树上染色
一道有机结合了计数和贪心这一DP两大考点的神仙题,不得不说做法是很玄妙. 首先我们很容易想到DP,设\(f_{i,j}\)表示在以\(i\)为根节点的子树中选\(j\)个黑色节点的最大收益值. 然后我 ...
- BJOI2018简要题解
BJOI2018简要题解 D1T1 二进制 题意 pupil 发现对于一个十进制数,无论怎么将其的数字重新排列,均不影响其是不是 \(3\) 的倍数.他想研究对于二进制,是否也有类似的性质. 于是他生 ...
- 《Head First 设计模式》例子的C++实现(1 策略模式)
最近在学习设计模式,用的是 <Head First 设计模式>这本书.感觉这本书写的还是很不错的,深入浅出的介绍了各种常用的设计模式.唯一有点不方便的地方是这本书的例子全都是用的 Java ...
- ActiveMQ在C#中的应用
本文是在.NET Framework框架下的应用,截止到目前ActiveMQ还不支持.NET Core,而RabbitMQ已经支持.NET Core,希望ActiveMQ能尽快支持. ActiveMQ ...
- maven项目中 org.hibernate.MappingNotFoundException: resource:*.hbm.xml not found问题的解决方案
是因为*.hbm.xml没有放到resource的mapper下导致的 对于Maven工程,编译的工作是由Maven程序来完成的,而Maven默认只会把src/main/resources文件夹下的文 ...
- 事件(event)
事件概述 委托是一种类型可以被实例化,而事件可以看作将多播委托进行封装的一个对象成员(简化委托调用列表增加和删除方法)但并非特殊的委托,保护订阅互不影响. 基础事件(event) 在.Net中声明事件 ...
- RetrieveFavicon 获取任何站点的 favicon
原文发表于我的技术博客 开源了一个获取任何站点 favicon 的类库,供使用. 原文发表于我的技术博客 RetrieveFavicon Project GitHub Retrieve favicon ...