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 ...
随机推荐
- Luogu2469 SDOI2010 星际竞速 费用流
传送门 发现它的本质是求一个费用最小的路径覆盖 最小路径覆盖是网络流23题中的一个比较典型的模型 所以考虑相似的建边 因为每一个点要恰好经过一次,是一个有上下界的网络流,故拆点,星球\(i\)拆成\( ...
- Your ApplicationContext is unlikely to start due to a @ComponentScan of the default package
1.在搭建SpringBoot框架时碰到的问题. ** WARNING ** : Your ApplicationContext is unlikely to start due to a @Comp ...
- [Spark][Python][DataFrame][SQL]Spark对DataFrame直接执行SQL处理的例子
[Spark][Python][DataFrame][SQL]Spark对DataFrame直接执行SQL处理的例子 $cat people.json {"name":" ...
- [Spark][Hive]Hive的命令行客户端启动:
[Spark][Hive]Hive的命令行客户端启动: [training@localhost Desktop]$ chkconfig | grep hive hive-metastore 0:off ...
- TDD、BDD、ATDD、DDD 软件开发模式
TDD.BDD.ATDD.DDD 软件开发模式 四个开发模式意思: TDD:测试驱动开发(Test-Driven Development) BDD:行为驱动开发(Behavior Driven Dev ...
- C语言----数据类型(基础篇一)
C语言的入门程序模板 #include <stdio.h> /*使用或者包含系统里面的程序*/ main() /*程序入口点*/ { /*起点*/ +; /*叫计算机执行的指令*/ } / ...
- gitblit 配置图文详解
Windows平台下Git服务器搭建 前提是确保存在JDK环境. 第一步:下载Gitblit.下载地址:http://www.gitblit.com/ 第二步:解压缩下载的压缩包即可,无需安装. 第三 ...
- CF 1047 C. Enlarge GCD
传送门 [http://codeforces.com/contest/1047/problem/C] 题意 给你n个数,移除最少的数字使剩下的数字GCD大于初始GCD 思路 需要一点暴力的技巧,先求出 ...
- 自己搭建的一个react脚手架
包括了: react.react router(v4), webpack(v4),echarts, google的组件库material ui, 后期会加上redux但是这些做中小型系统已经够了,de ...
- junit-test
一.题目简介: 用单元测试junit4测试calculator类的加减乘除四种方法,来初步学习junit4的学习方法. 二.源码的github链接 :https://github.com/weare ...