H. Special Palindrome

time limit per test:1 second
memory limit per test:64 megabytes
input:standard input
output:standard output

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).

Examples
Input
1
3
7
10
0
Output
1
2
5
17

题目链接:http://codeforces.com/gym/100952/problem/H

题意:一个从开始到中间是非递减的回文被称为特殊回文,例如1123211,定义F(N)为和为N的特殊回文的个数,如F(1)=1,即和为1的回文只有一个 就是 1,F(5)=7, (7), (1 5 1), (2 3 2), (1 1 3 1 1), (1 1 1 1 1 1 1),求F(N),N小于等于250!

思路:当N为偶数时,分2种情况,第一种为回文的长度为奇数,那么,最中间的数 m 一定是2 4 6 8......两边的数的和为(N-m)>>1,对(N-i)>>1进行整数划分(m划分),第二种为回文长度为偶数,则回文两边的和为N>>1,对N>>1整数划分(N>>1划分),当N为奇数的时候只有一种情况,就是回文长度为奇数,最中间的数m为1 3 5 7....划分和上面一样!

下面给出AC代码:

 #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
inline ll read()
{
ll x=,f=;
char ch=getchar();
while(ch<''||ch>'')
{
if(ch=='-')
f=-;
ch=getchar();
}
while(ch>=''&&ch<='')
{
x=x*+ch-'';
ch=getchar();
}
return x*f;
}
inline void write(ll x)
{
if(x<)
{
putchar('-');
x=-x;
}
if(x>)
write(x/);
putchar(x%+'');
}
ll dp[][];
inline void funday(ll n)
{
for(ll i=;i<=n;i++)
{
for(ll j=;j<=n;j++)
{
if(i==||j==)
dp[i][j]=;
else if(i>j)
dp[i][j]=dp[i-j][j]+dp[i][j-];
else if(i==j)
dp[i][j]=dp[i][i-]+;
else dp[i][j]=dp[i][i];
}
}
}
ll n,m,ans;
int main()
{
funday();
while(n=read())
{
if(n==)
return ;
ans=;
if(n&)
{
for(ll i=;i<=n;i+=)
ans+=dp[(n-i)/][i];
ans++;
}
else
{
for(ll i=;i<=n;i+=)
ans+=dp[(n-i)/][i];
ans+=dp[n/][n/];
ans++;
}
write(ans);
printf("\n");
}
return ;
}

打表解法:

分奇偶用 dfs 搞出非递减的左半边串, 然后求出这个的和 ans[sum + i]++;

对于偶数个的直接dfs, 对于奇数的则枚举mid, 然后依次dfs

 void dfseven(int k, int sum)
{
if(*sum > ) return;
//cout<<"here1"<<endl;
for(int i = k; i <= ; i++){
if(*(sum + i) <= ) {ans[*(sum + i)]++; dfseven(i, sum + i);}
else return;
} } void dfsodd(int mid, int k, int sum)
{
if(*sum + mid > ) return;
//cout<<"here2"<<endl;
for(int i = k; i <= ; i++){
if(*(sum + i) + mid <= && i <= mid) {ans[*(sum + i) + mid]++; dfsodd(mid, i, sum + i);}
else return;
} }

然后只打了前ans[50] 及以前的, 因为后面的比较大时间不够的, 所以打出前50的表然后到数列网站 OEIS 查了一下, 还真有,??

所以把那前250个ans贴到 txt里, 然后写一个中间程序 把这些数据 转换成 printf("ans[%d] = %d;", i, val);的样子打到另一个txt文件里, 然后复杂粘贴到上去, 整理下就好了

打表完整代码:

 #include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long LL;
const int maxn = + ; int ans[maxn]; //偶数个的
void dfseven(int k, int sum)
{
if(*sum > ) return;
//cout<<"here1"<<endl;
for(int i = k; i <= ; i++){
if(*(sum + i) <= ) {ans[*(sum + i)]++; dfseven(i, sum + i);}
else return;
} } void dfsodd(int mid, int k, int sum)
{
if(*sum + mid > ) return;
//cout<<"here2"<<endl;
for(int i = k; i <= ; i++){
if(*(sum + i) + mid <= && i <= mid) {ans[*(sum + i) + mid]++; dfsodd(mid, i, sum + i);}
else return;
} } int main()
{
#ifdef LOCAL
freopen("a.txt", "r", stdin);
freopen("b.txt", "w", stdout);
#endif // LOCAL
memset(ans, , sizeof ans);
//ans[2]++;
dfseven(, );
//ans[1]++;
for(int i = ; i <= ; i++){
dfsodd(i, , );
}
for(int i = ; i <= ; i++){
printf("ans[%d] = %d;", i, ans[i] + );
} /*
int n;
while(scanf("%d", &n)){
printf("%d", ans[n]); }
*/
return ;
}

最终AC代码如下:

 #include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long LL;
const int maxn = + ; LL ans[maxn]; int main()
{ ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
ans[] = ;
int n;
while(scanf("%d", &n)){
if(n == ) break;
printf("%I64d\n", ans[n]);
} return ;
}

Gym 100952H&&2015 HIAST Collegiate Programming Contest H. Special Palindrome【dp预处理+矩阵快速幂/打表解法】的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 2015 HIAST Collegiate Programming Contest H

    A sequence of positive and non-zero integers called palindromic if it can be read the same forward a ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. Gym 100952B&&2015 HIAST Collegiate Programming Contest B. New Job【模拟】

    B. New Job time limit per test:1 second memory limit per test:64 megabytes input:standard input outp ...

随机推荐

  1. 【JS】数据类型

    其他类型转化为boolean类型规则: number:非0为true,0和NaN为false: String:非空为true,空为false: Object:任何对象都为true 任何变量赋值为nul ...

  2. JNI的使用总结初篇

    前言:以下内容是个人在写JNI Demo前后进行查找理解总结得出的一些结论,如有错误的地方希望路过的朋友能够指正. 一.JNI是Java native interface的简称,目前就我所知这类方法的 ...

  3. VS源码编译QuaZip(Windows下)

    最近写个Qt demo,想要使用压缩和解压多个文件的功能,并不使用额外进程.网上参考了很多资料,发现只有QuaZip比较适合我的需求.但是QuaZip只提供源码,因此需要自己来编译. QuaZip简介 ...

  4. Tensorflow之MNIST机器学习入门

    MNIST机器学习的原理: 通过一次次的 输入某张图片的像素值(用784维向量表示)以及这张图片对应的数字(用10维向量表示比如数字1用[0,1,0,0,0,0,0,0,0,0]表示),来优化10*7 ...

  5. ElasticSearch 学习记录之父子结构的查询

    父子结构 父亲type属性查询子type 的类型 父子结构的查询,可以通过父亲类型的字段,查询出子类型的索引信息 POST /product/_search { "query": ...

  6. html统计

    <!doctype html><html lang="en"> <head>  <meta charset="UTF-8&quo ...

  7. tar: This does not look like a tar archive tar: Skipping to next header tar: Exiting with failure status due to previous errors

    解压一个.tar.zip文件时报错 tar -zxvf bcl2fastq2-v2---linux-x86-.zip tar: This does not look like a tar archiv ...

  8. idea激活网站地址,亲测可用(windows7,idea 2016)

    help-register-license server,然后输入 http://idea.iteblog.com/key.php

  9. 程序包管理rpm、yum与简单编译安装程序

    Linux程序包管理 Linux中软件的安装主要有两种形式:一种是直接下载源代码包自行编译后安装,另一种直接获取rpm软件包进行安装. 程序的组成部分: 二进制程序:程序的主体文件,比如我们运行一个l ...

  10. python的defaultdict

    defaultdict是dict的一个子类,接受一个工厂函数作为参数,当访问defaultdict中不存在的key时,会将工厂函数的返回值作为默认的value. class defaultdict(d ...