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. 1
    3
    7
    10
    0
Output
  1. 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代码:

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. inline ll read()
  5. {
  6. ll x=,f=;
  7. char ch=getchar();
  8. while(ch<''||ch>'')
  9. {
  10. if(ch=='-')
  11. f=-;
  12. ch=getchar();
  13. }
  14. while(ch>=''&&ch<='')
  15. {
  16. x=x*+ch-'';
  17. ch=getchar();
  18. }
  19. return x*f;
  20. }
  21. inline void write(ll x)
  22. {
  23. if(x<)
  24. {
  25. putchar('-');
  26. x=-x;
  27. }
  28. if(x>)
  29. write(x/);
  30. putchar(x%+'');
  31. }
  32. ll dp[][];
  33. inline void funday(ll n)
  34. {
  35. for(ll i=;i<=n;i++)
  36. {
  37. for(ll j=;j<=n;j++)
  38. {
  39. if(i==||j==)
  40. dp[i][j]=;
  41. else if(i>j)
  42. dp[i][j]=dp[i-j][j]+dp[i][j-];
  43. else if(i==j)
  44. dp[i][j]=dp[i][i-]+;
  45. else dp[i][j]=dp[i][i];
  46. }
  47. }
  48. }
  49. ll n,m,ans;
  50. int main()
  51. {
  52. funday();
  53. while(n=read())
  54. {
  55. if(n==)
  56. return ;
  57. ans=;
  58. if(n&)
  59. {
  60. for(ll i=;i<=n;i+=)
  61. ans+=dp[(n-i)/][i];
  62. ans++;
  63. }
  64. else
  65. {
  66. for(ll i=;i<=n;i+=)
  67. ans+=dp[(n-i)/][i];
  68. ans+=dp[n/][n/];
  69. ans++;
  70. }
  71. write(ans);
  72. printf("\n");
  73. }
  74. return ;
  75. }

打表解法:

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

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

  1. void dfseven(int k, int sum)
  2. {
  3. if(*sum > ) return;
  4. //cout<<"here1"<<endl;
  5. for(int i = k; i <= ; i++){
  6. if(*(sum + i) <= ) {ans[*(sum + i)]++; dfseven(i, sum + i);}
  7. else return;
  8. }
  9.  
  10. }
  11.  
  12. void dfsodd(int mid, int k, int sum)
  13. {
  14. if(*sum + mid > ) return;
  15. //cout<<"here2"<<endl;
  16. for(int i = k; i <= ; i++){
  17. if(*(sum + i) + mid <= && i <= mid) {ans[*(sum + i) + mid]++; dfsodd(mid, i, sum + i);}
  18. else return;
  19. }
  20.  
  21. }

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

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

打表完整代码:

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. using namespace std;
  5. typedef long long LL;
  6. const int maxn = + ;
  7.  
  8. int ans[maxn];
  9.  
  10. //偶数个的
  11. void dfseven(int k, int sum)
  12. {
  13. if(*sum > ) return;
  14. //cout<<"here1"<<endl;
  15. for(int i = k; i <= ; i++){
  16. if(*(sum + i) <= ) {ans[*(sum + i)]++; dfseven(i, sum + i);}
  17. else return;
  18. }
  19.  
  20. }
  21.  
  22. void dfsodd(int mid, int k, int sum)
  23. {
  24. if(*sum + mid > ) return;
  25. //cout<<"here2"<<endl;
  26. for(int i = k; i <= ; i++){
  27. if(*(sum + i) + mid <= && i <= mid) {ans[*(sum + i) + mid]++; dfsodd(mid, i, sum + i);}
  28. else return;
  29. }
  30.  
  31. }
  32.  
  33. int main()
  34. {
  35. #ifdef LOCAL
  36. freopen("a.txt", "r", stdin);
  37. freopen("b.txt", "w", stdout);
  38. #endif // LOCAL
  39. memset(ans, , sizeof ans);
  40. //ans[2]++;
  41. dfseven(, );
  42. //ans[1]++;
  43. for(int i = ; i <= ; i++){
  44. dfsodd(i, , );
  45. }
  46. for(int i = ; i <= ; i++){
  47. printf("ans[%d] = %d;", i, ans[i] + );
  48. }
  49.  
  50. /*
  51. int n;
  52. while(scanf("%d", &n)){
  53. printf("%d", ans[n]);
  54.  
  55. }
  56. */
  57. return ;
  58. }

最终AC代码如下:

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. using namespace std;
  5. typedef long long LL;
  6. const int maxn = + ;
  7.  
  8. LL ans[maxn];
  9.  
  10. int main()
  11. {
  12.  
  13. ans[] = ;
  14. ans[] = ;
  15. ans[] = ;
  16. ans[] = ;
  17. ans[] = ;
  18. ans[] = ;
  19. ans[] = ;
  20. ans[] = ;
  21. ans[] = ;
  22. ans[] = ;
  23. ans[] = ;
  24. ans[] = ;
  25. ans[] = ;
  26. ans[] = ;
  27. ans[] = ;
  28. ans[] = ;
  29. ans[] = ;
  30. ans[] = ;
  31. ans[] = ;
  32. ans[] = ;
  33. ans[] = ;
  34. ans[] = ;
  35. ans[] = ;
  36. ans[] = ;
  37. ans[] = ;
  38. ans[] = ;
  39. ans[] = ;
  40. ans[] = ;
  41. ans[] = ;
  42. ans[] = ;
  43. ans[] = ;
  44. ans[] = ;
  45. ans[] = ;
  46. ans[] = ;
  47. ans[] = ;
  48. ans[] = ;
  49. ans[] = ;
  50. ans[] = ;
  51. ans[] = ;
  52. ans[] = ;
  53. ans[] = ;
  54. ans[] = ;
  55. ans[] = ;
  56. ans[] = ;
  57. ans[] = ;
  58. ans[] = ;
  59. ans[] = ;
  60. ans[] = ;
  61. ans[] = ;
  62. ans[] = ;
  63. ans[] = ;
  64. ans[] = ;
  65. ans[] = ;
  66. ans[] = ;
  67. ans[] = ;
  68. ans[] = ;
  69. ans[] = ;
  70. ans[] = ;
  71. ans[] = ;
  72. ans[] = ;
  73. ans[] = ;
  74. ans[] = ;
  75. ans[] = ;
  76. ans[] = ;
  77. ans[] = ;
  78. ans[] = ;
  79. ans[] = ;
  80. ans[] = ;
  81. ans[] = ;
  82. ans[] = ;
  83. ans[] = ;
  84. ans[] = ;
  85. ans[] = ;
  86. ans[] = ;
  87. ans[] = ;
  88. ans[] = ;
  89. ans[] = ;
  90. ans[] = ;
  91. ans[] = ;
  92. ans[] = ;
  93. ans[] = ;
  94. ans[] = ;
  95. ans[] = ;
  96. ans[] = ;
  97. ans[] = ;
  98. ans[] = ;
  99. ans[] = ;
  100. ans[] = ;
  101. ans[] = ;
  102. ans[] = ;
  103. ans[] = ;
  104. ans[] = ;
  105. ans[] = ;
  106. ans[] = ;
  107. ans[] = ;
  108. ans[] = ;
  109. ans[] = ;
  110. ans[] = ;
  111. ans[] = ;
  112. ans[] = ;
  113. ans[] = ;
  114. ans[] = ;
  115. ans[] = ;
  116. ans[] = ;
  117. ans[] = ;
  118. ans[] = ;
  119. ans[] = ;
  120. ans[] = ;
  121. ans[] = ;
  122. ans[] = ;
  123. ans[] = ;
  124. ans[] = ;
  125. ans[] = ;
  126. ans[] = ;
  127. ans[] = ;
  128. ans[] = ;
  129. ans[] = ;
  130. ans[] = ;
  131. ans[] = ;
  132. ans[] = ;
  133. ans[] = ;
  134. ans[] = ;
  135. ans[] = ;
  136. ans[] = ;
  137. ans[] = ;
  138. ans[] = ;
  139. ans[] = ;
  140. ans[] = ;
  141. ans[] = ;
  142. ans[] = ;
  143. ans[] = ;
  144. ans[] = ;
  145. ans[] = ;
  146. ans[] = ;
  147. ans[] = ;
  148. ans[] = ;
  149. ans[] = ;
  150. ans[] = ;
  151. ans[] = ;
  152. ans[] = ;
  153. ans[] = ;
  154. ans[] = ;
  155. ans[] = ;
  156. ans[] = ;
  157. ans[] = ;
  158. ans[] = ;
  159. ans[] = ;
  160. ans[] = ;
  161. ans[] = ;
  162. ans[] = ;
  163. ans[] = ;
  164. ans[] = ;
  165. ans[] = ;
  166. ans[] = ;
  167. ans[] = ;
  168. ans[] = ;
  169. ans[] = ;
  170. ans[] = ;
  171. ans[] = ;
  172. ans[] = ;
  173. ans[] = ;
  174. ans[] = ;
  175. ans[] = ;
  176. ans[] = ;
  177. ans[] = ;
  178. ans[] = ;
  179. ans[] = ;
  180. ans[] = ;
  181. ans[] = ;
  182. ans[] = ;
  183. ans[] = ;
  184. ans[] = ;
  185. ans[] = ;
  186. ans[] = ;
  187. ans[] = ;
  188. ans[] = ;
  189. ans[] = ;
  190. ans[] = ;
  191. ans[] = ;
  192. ans[] = ;
  193. ans[] = ;
  194. ans[] = ;
  195. ans[] = ;
  196. ans[] = ;
  197. ans[] = ;
  198. ans[] = ;
  199. ans[] = ;
  200. ans[] = ;
  201. ans[] = ;
  202. ans[] = ;
  203. ans[] = ;
  204. ans[] = ;
  205. ans[] = ;
  206. ans[] = ;
  207. ans[] = ;
  208. ans[] = ;
  209. ans[] = ;
  210. ans[] = ;
  211. ans[] = ;
  212. ans[] = ;
  213. ans[] = ;
  214. ans[] = ;
  215. ans[] = ;
  216. ans[] = ;
  217. ans[] = ;
  218. ans[] = ;
  219. ans[] = ;
  220. ans[] = ;
  221. ans[] = ;
  222. ans[] = ;
  223. ans[] = ;
  224. ans[] = ;
  225. ans[] = ;
  226. ans[] = ;
  227. ans[] = ;
  228. ans[] = ;
  229. ans[] = ;
  230. ans[] = ;
  231. ans[] = ;
  232. ans[] = ;
  233. ans[] = ;
  234. ans[] = ;
  235. ans[] = ;
  236. ans[] = ;
  237. ans[] = ;
  238. ans[] = ;
  239. ans[] = ;
  240. ans[] = ;
  241. ans[] = ;
  242. ans[] = ;
  243. ans[] = ;
  244. ans[] = ;
  245. ans[] = ;
  246. ans[] = ;
  247. ans[] = ;
  248. ans[] = ;
  249. ans[] = ;
  250. ans[] = ;
  251. ans[] = ;
  252. ans[] = ;
  253. ans[] = ;
  254. ans[] = ;
  255. ans[] = ;
  256. ans[] = ;
  257. ans[] = ;
  258. ans[] = ;
  259. ans[] = ;
  260. ans[] = ;
  261. ans[] = ;
  262. ans[] = ;
  263. int n;
  264. while(scanf("%d", &n)){
  265. if(n == ) break;
  266. printf("%I64d\n", ans[n]);
  267. }
  268.  
  269. return ;
  270. }

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. Python 集体智慧编程PDF

    集体智慧编程PDF 1.图书思维导图http://www.pythoner.com/183.html p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12. ...

  2. ABP 用swagger UI测试API报401无权限访问问题

    问题描述: 当我们用swagger UI对Web API 进行测试时报401错误 我们点开GET /api/services/app/Role/GetAll,输入参数 点击Try it out!按钮, ...

  3. k-近邻算法实例

    1. 简单例子 步骤 1.1 计算已知点和被求点的距离 1.2 按距离递增排序 1.3 求出距离最近的前k个点的类别最大值作为目标分类 from numpy import * import opera ...

  4. 在windows 10下安装python

    windows系统默认状态下是没有安装python的,我们需要下载并安装它. 首先检查是否安装了python 在"开始"菜单中输入cmd,然后右击选择管理员身份运行,这样就打开了一 ...

  5. git正确的删除远程仓库的文件并用.gitignore忽略提交此文件

    我向远程仓库提交了如下文件src/ pom.xml target/ WebContent/,发现没必要提交target目录. 于是做了如下操作: git rm -r --cached target g ...

  6. css3 结构性伪类选择器

    伪类 选择器 类型 说明 备注 E:first-line 伪元素选择器 选择匹配E元素内的第一行文本 E:first-letter 伪元素选择器 选择匹配E元素内的第一个字符 E:before 伪元素 ...

  7. UNIX域协议(命名套接字)

    这里主要介绍命名UNIX域套接字 1.什么是UNIX域套接字Unix域协议并不是一个实际的协议族,而是在单个主机上执行客户/服务通信的一种方式.是进程间通信(IPC)的一种方式.它提供了两类套接字:字 ...

  8. 高度-宽度关系,同一div、不同div高度与宽度关系控制函数

    //对象1的高度等于对象2的高度n倍,调用方法:Ht1DivideHt2('#div2','#div1',3)//div2的高度是div1高度的3倍function Ht1DivideHt2(obj1 ...

  9. h5拖拽上传图片

    h5实现拖拽上传图片 本文将为大家介绍如何通过js实现拖拽上传图片. 首先我们要禁用调浏览器默认的拖拽事件: window.onload = function(){ //拖离 document.add ...

  10. iOS中的armv7,armv7s,arm64,i386,x86_64

    前言 一般iOS中的armv7.armv7s.arm64.i386.x86_64这些都代表了什么?在Xcode中如何选择? 介绍 armv7.armv7s.arm64都是ARM处理器的指令集. i38 ...