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 ...
随机推荐
- iOS 获取当前应用的信息以及用户信息:版本号手机号手机型号
NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary]; CFShow(infoDictionary); // ap ...
- 用过的关于css的知识
1.代码片段 让两个div并排起来显示. <div style="width:1000px; text-align:center;" id="content&quo ...
- Linux(CentOS7.1)修改默认yum源为国内的阿里云yum源
官方的yum源在国内访问效果不佳. 需要改为国内比较好的阿里云或者网易的yum源 修改方式: 下载wget yum install wget -y echo 备份当前的yum源 mv /etc/yum ...
- python net-snmp 的使用
这一年一直在做一个综合管控平台的项目,用python写的,项目春节前可能就要进行实际部署了和测试,趁着这个空闲期,回顾一下项目中用到的一些技术,第一个就是SNMP协议. 项目结构主要是实现对ipran ...
- a:hover标签已经定义了text-decoration:none,并且生效,但是还是有下划线
a标签在F12计算出来的样式里 text-decoration:none; 确实有被应用到.但是链接的下划线并没有被去掉... 解决办法:p:commandLink <p:commandLink ...
- fastq,sam文件一些小结(持续补充。。。)
ST-E00211::H5L3NCCXY:::: chr14 141M = - ACTTCACCTCCTGGAGTCCTGGACTTCCCCACATCTCCCCTGCCCCTCCCACGTTTCCAT ...
- 2018年第一篇行动笔记:Reading Log
今天读了盖兆泉的文章<美国教师怎么上阅读课>,觉得干货颇多,不仅仅针对儿童英语阅读,而且对生活的方方面面都有助益. 该文章主要内容摘要如下: 学生需要大量时间阅读 这里的阅读时间是特指花在 ...
- K:java序列化与反序列化—transient关键字的使用
首先,应该明白的是transient是java中的一个关键字,音标为 英: [ˈtrænziənt]. 在了解transient关键字之前,应该先弄明白序列化和反序列化.所谓的序列化,通俗点的 ...
- WebDriver的等待方式
/* * 1.线程休眠 * 2.隐式等待 * 3.显示等待 * */ package com.sfwork; import java.util.concurrent.TimeUnit; import ...
- icon图标和文字整体居中在button按钮
icon图标和文字整体居中在button按钮 icon图标和文字整体居中 一般我们常做的button按钮是文字居中 现在这个需要icon图标和文字一起居中在背景色 <a href="# ...