Gym 100952H&&2015 HIAST Collegiate Programming Contest H. Special Palindrome【dp预处理+矩阵快速幂/打表解法】
H. Special Palindrome
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.
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.
Print one line for each given number N, which it the value F(N).
1
3
7
10
0
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预处理+矩阵快速幂/打表解法】的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- ASP.NET Core使用SkiaSharp实现验证码
前言 本文并没有实现一个完成的验证码样例,只是提供了在当前.NET Core 2.0下使用Drawing API的另一种思路,并以简单Demo的形式展示出来. Skia Skia是一个开源的二维图形库 ...
- Python 集体智慧编程PDF
集体智慧编程PDF 1.图书思维导图http://www.pythoner.com/183.html p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12. ...
- android 串口开发第一篇:搭建ndk开发环境以及第一个jni调用程序
一:ndk环境搭建 1:开发环境 我使用的是android studio 2.3.3版本,搭建ndk开发环境比较简单,打开File----Settings----Appearance&Beha ...
- 关于MAX()函数的一点思考
本文同时发表在https://github.com/zhangyachen/zhangyachen.github.io/issues/103 考虑如下表和sql: CREATE TABLE `ikno ...
- seo我告诉你
seo我告诉你,这回seo真的告诉你百度云链接 链接:http://pan.baidu.com/s/1qYpM9y8 密码:mad6 seo优化教程:
- canvas学习api
1.canvas.getContext():获取渲染上下文和绘画功能: 一.绘制矩形 2.ctx.fillRect(x,y,width,height):绘制矩形: 3.ctx.strokeRect(x ...
- Asp,NET控制文件上传的大小
在web.config中的system.web 节点下添加如下代码: 第2行的maxRequestLength="8192",这里限制最大为8MB,可以自行设置.execution ...
- StringMVC @RequestParam属性
1.jsp: <a href="springmvc/testRequestParam?username=allen&age=sss">test RequsetP ...
- 用Python让单片机“行动”起来——MicroPython实战入门篇
MicroPython以微控制器作为目标,从而使得Python可以用来控制硬件.说到MicroPython,也许有人会感到陌生.而说到和它密切相关的Python,是否会恍然大悟呢?Python属于解释 ...
- 【图文】如何在centos上安装tomcat
先到tomcat官网下载安装包(随便下载你想要的版本) 假设你现在使用的是windows系统 那么就把你下载来的压缩包解压,放到一个目录中 在你本地的windows系统中安装个xshell和xftp ...