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 ...
随机推荐
- Java IO学习要点导图
Java IO的一些基础知识: 导图源文件保存地址:https://github.com/wanghaoxi3000/xmind
- sqlserver 存储过程 递归查询分组+hierarchyid重建会员关系
CREATE PROCEDURE [dbo].[GetGroupInfo] @s_code NVARCHAR() = --会员卡号 AS BEGIN declare @p int; --查询唯一性结果 ...
- AutoLayout的几种方法
1.XIB 2.Fram 3.屏幕比例适配(个人比较推荐) iOS屏幕适配(尺寸适配) 4.NSLayoutConstraint. 5.Masonry 概述 使用 Objective-C 纯代码编 ...
- ReactNative实现图集功能
需求描述: 图片缩放.拖动.长按保存等基础图片查看的功能: 展示每张图片文本描述: 实现效果,如图: 实现步骤 使用第三方插件:react-native-image-zoom-viewer 插件Git ...
- JDK动态代理[1]----代理模式实现方式的概要介绍
日常工作中经常会接触到代理模式,但一直没有对其进行深究.代理模式一直就像一团迷雾一样存在我心里,什么是代理模式?为什么要使用代理?代理模式有哪些实现?它的底层机制是怎样的?这些问题促使着我迫切想要揭开 ...
- Pashmak and Flowers
Pashmak decided to give Parmida a pair of flowers from the garden. There are nflowers in the garden ...
- 4.Nginx的URL重写应用
Nginx的URL重写应用 nginx的URL重写模块是用得比较多的模块之一,所以我们需要好好地掌握运用.常用的URL重写模块命令有if,rewrite,set,break等. if命令 if用于判断 ...
- linux下后台运行MATLAB
原帖:http://sypeterli1.blog.163.com/blog/static/2283740492013101745824207/ 后台运行matlab脚本文件的方法:nohup ...
- Illustration of Git branching and merge
网上看到的描述Git工作流程的图片,有些出处忘了保存,仅供学习. 1. One Git Branching Model 出处: http://nvie.com/posts/a-successful-g ...
- Linux(CentOS6.5)下编译安装PHP5.6.22时报错”configure: error: ZLIB extension requires gzgets in zlib”的解决方式(确定已经编译安装Zlib,并已经指定Zlib路径)
本文地址http://comexchan.cnblogs.com/,作者Comex Chan,尊重知识产权,转载请注明出处,谢谢! 今天在CentOS6.5下编译安装PHP时,一直报错 confi ...