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 ...
随机推荐
- puppet配置问题统计
一. [root@client puppet]# puppetd --test --server master.test.cominfo: Creating a new SSL key for cli ...
- MySQL创建一个固定频率执行且自定义"开始"时间的定时任务event
drop event if exists evt_test;create event evt_teston schedule every 10 SECOND -- 每10秒执行一次(second可以 ...
- linux命令(shell)
1.cat查看一个文件,linux默认bash 2.echo回显命令 3.ls 4.history历史记录,查看使用过的命令 5.根目录下文件目录 6.bin目录下内容多为应用程序和命令 7.boot ...
- MySQL index 增删改
一.前提信息 1.数据库版本 mysql> select version(),user(); +------------+----------------+ | version() | user ...
- HTTPS从认识到线上实战全记录
前言 关于HTTPS,基本上你想知道的都在这里了.本文原标题<HTTPS原理与实践>,下图是本文配套PPT的目录截图: [TOC] 原理篇 认识HTTPS 先说一下,本文可能有些地方由于描 ...
- jquery隐藏域赋值
<input type="hidden" id="id" value="value"> $("#id的值") ...
- Odwiedziny[POI 2015]
题目描述 给定一棵n个点的树,树上每条边的长度都为1,第i个点的权值为a[i]. Byteasar想要走遍这整棵树,他会按照某个1到n的全排列b走n-1次,第i次他会从b[i]点走到b[i+1]点,并 ...
- java 类方法和实例方法 以及 类变量和实例变量
类体中的方法分为实例方法和类方法两种,用static修饰的是类方法 类方法: 对于类中的类方法,在该类被加载到内存时,就分配了相应的入口地址.从而类方法不仅可以被类创建的任何对象调用执行,也可以直接通 ...
- Vue 组件(component)之 精美的日历
公司的要求,需要开发一个精美的日历组件(IOS , 安卓, PC 的IE9+都能运行),写完后想把它分享出来,希望大家批评(). 先来个截图 代码已经分享到 https://github.com/zh ...
- python 打印几行空行、 打印不换行
2.x版本中使用print '\n' * n #n为行数 3.x版本中使用print('\n' * n) #n为行数 print 'Hello', :不会换行.[加上逗号(,)]