给你一个数 \(n\),求有多少种方案能把 \(n\) 分成两个非零回文数 \((a,b)\) 之和。

  两个方案不同当且仅当 \(a_1\neq a_2\)。

  \(n\leq {10}^{18}\)

题解

  枚举那些位进了位,然后分两种情况讨论:

  1.两个回文数位数相等。可以直接计算方案数。

  2.两个回文数位数不相等。可以枚举位数,构造方程,然后解出来。例如,记第一个回文数为 \((a_4a_3a_2a_1)_{10}\),第二个回文数为 \((b_3b_2b_1)_{10}\),\(n=(c_4c_3c_2c_1)_{10}\)。构造的方程为:

\[\begin{cases}
a_4&=c_4\\
a_3+b_3&=c_3\\
a_2+b_2&=c_2\\
a_1+b_1&=c_1\\
a_4&=a_1\\
a_3&=a_2\\
b_3&=b_1\\
\end{cases}
\]

  记 \(m=\log_{10}n\)。

  复杂度为 \(O(m^22^m)\)

  如果你只枚举前面 \(\frac{m}{2}\) 位是否进位,可以做到 \(O(m^22^{\frac{m}{2}})\)

代码

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<algorithm>
  4. #include<cstdlib>
  5. #include<ctime>
  6. #include<utility>
  7. #include<functional>
  8. #include<cmath>
  9. #include<vector>
  10. #include<assert.h>
  11. //using namespace std;
  12. using std::min;
  13. using std::max;
  14. using std::swap;
  15. using std::sort;
  16. using std::reverse;
  17. using std::random_shuffle;
  18. using std::lower_bound;
  19. using std::upper_bound;
  20. using std::unique;
  21. using std::vector;
  22. typedef long long ll;
  23. typedef unsigned long long ull;
  24. typedef double db;
  25. typedef std::pair<int,int> pii;
  26. typedef std::pair<ll,ll> pll;
  27. void open(const char *s){
  28. #ifndef ONLINE_JUDGE
  29. char str[100];sprintf(str,"%s.in",s);freopen(str,"r",stdin);sprintf(str,"%s.out",s);freopen(str,"w",stdout);
  30. #endif
  31. }
  32. void open2(const char *s){
  33. #ifdef DEBUG
  34. char str[100];sprintf(str,"%s.in",s);freopen(str,"r",stdin);sprintf(str,"%s.out",s);freopen(str,"w",stdout);
  35. #endif
  36. }
  37. int rd(){int s=0,c,b=0;while(((c=getchar())<'0'||c>'9')&&c!='-');if(c=='-'){c=getchar();b=1;}do{s=s*10+c-'0';}while((c=getchar())>='0'&&c<='9');return b?-s:s;}
  38. void put(int x){if(!x){putchar('0');return;}static int c[20];int t=0;while(x){c[++t]=x%10;x/=10;}while(t)putchar(c[t--]+'0');}
  39. int upmin(int &a,int b){if(b<a){a=b;return 1;}return 0;}
  40. int upmax(int &a,int b){if(b>a){a=b;return 1;}return 0;}
  41. ll ans;
  42. int t,t2;
  43. int a[100];
  44. int b[100];
  45. ll n;
  46. void gao1()
  47. {
  48. for(int i=1;i<=t2;i++)
  49. if(b[i]!=b[t2-i+1])
  50. return;
  51. ll res=1;
  52. for(int i=1;i<=(t2+1)/2;i++)
  53. res*=min(9,(i==1?b[i]-1:b[i]))-max(b[i]-9,(i==1?1:0))+1;
  54. ans+=res;
  55. }
  56. int a1[100],a2[100];
  57. void gao2()
  58. {
  59. for(int i=1;i<t2;i++)
  60. {
  61. for(int j=1;j<=t2;j++)
  62. a1[j]=a2[j]=0;
  63. for(int j=t2;j>i;j--)
  64. if(!a1[j])
  65. {
  66. int x=j;
  67. int v=b[j];
  68. while(!a1[x])
  69. {
  70. a1[x]=v;
  71. x=t2-x+1;
  72. a1[x]=v;
  73. if(a2[x])
  74. break;
  75. if(x>i)
  76. break;
  77. a2[x]=v=b[x]-a1[x];
  78. x=i-x+1;
  79. a2[x]=v;
  80. v=b[x]-a2[x];
  81. }
  82. }
  83. int flag=1;
  84. for(int j=1;flag&&j<=t2;j++)
  85. if(a1[j]<0||a1[j]>9||a1[j]+a2[j]!=b[j]||a1[t2-j+1]!=a1[j])
  86. flag=0;
  87. for(int j=1;flag&&j<=i;j++)
  88. if(a2[j]<0||a2[j]>9||a2[j]!=a2[i-j+1])
  89. flag=0;
  90. if(a2[i]==0)
  91. flag=0;
  92. if(flag)
  93. ans+=2;
  94. }
  95. }
  96. int main()
  97. {
  98. open("number");
  99. scanf("%lld",&n);
  100. ll m=n;
  101. while(m)
  102. {
  103. a[++t]=m%10;
  104. m/=10;
  105. }
  106. for(int i=0;i<1<<(t-1);i++)
  107. {
  108. for(int j=1;j<=t;j++)
  109. b[j]=a[j];
  110. for(int j=1;j<=t;j++)
  111. if((i>>(j-1))&1)
  112. {
  113. b[j]+=10;
  114. b[j+1]--;
  115. }
  116. int flag=1;
  117. for(int j=1;j<=t;j++)
  118. if(b[j]<0||b[j]>18)
  119. {
  120. flag=0;
  121. break;
  122. }
  123. if(!flag)
  124. continue;
  125. t2=t;
  126. while(!b[t2])
  127. t2--;
  128. gao1();
  129. gao2();
  130. }
  131. printf("%lld\n",ans);
  132. return 0;
  133. }

【SEERC2018A】【XSY3319】Numbers的更多相关文章

  1. 【BZOJ1662】[Usaco2006 Nov]Round Numbers 圆环数 数位DP

    [BZOJ1662][Usaco2006 Nov]Round Numbers 圆环数 Description 正如你所知,奶牛们没有手指以至于不能玩"石头剪刀布"来任意地决定例如谁 ...

  2. 【LeetCode-面试算法经典-Java实现】【129-Sum Root to Leaf Numbers(全部根到叶子结点组组成的数字相加)】

    [129-Sum Root to Leaf Numbers(全部根到叶子结点组组成的数字相加)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a bina ...

  3. 【数位dp】CF 55D Beautiful numbers

    题目 Volodya is an odd boy and his taste is strange as well. It seems to him that a positive integer n ...

  4. 【LeetCode】165. Compare Version Numbers 解题报告(Python)

    [LeetCode]165. Compare Version Numbers 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...

  5. UVa 11582 Colossal Fibonacci Numbers! 【大数幂取模】

    题目链接:Uva 11582 [vjudge] watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fil ...

  6. Python之路【第十八篇】:Web框架们

    Python之路[第十八篇]:Web框架们   Python的WEB框架 Bottle Bottle是一个快速.简洁.轻量级的基于WSIG的微型Web框架,此框架只由一个 .py 文件,除了Pytho ...

  7. 【DFS深搜初步】HDOJ-2952 Counting Sheep、NYOJ-27 水池数目

    [题目链接:HDOJ-2952] Counting Sheep Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 ...

  8. 【POI2003/2004 stage I】

    [原题在此] Let us consider a game on a rectangular board m x 1 consisting of m elementary squares number ...

  9. POJ 3414 Pots【bfs模拟倒水问题】

    链接: http://poj.org/problem?id=3414 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22009#probl ...

  10. 【LeetCode题意分析&解答】40. Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

随机推荐

  1. 上传图片截图预览控件不显示cropper.js 跨域问题

    上传图片到图片服务器,因为域名不同,多以会有跨域问题. No 'Access-Control-Allow-Origin' header is present on the requested reso ...

  2. Java 8中Stream API学习笔记

    1)函数式编程的优势和劣势分别是什么?优势:①不可变性 ②并行操作 ③执行顺序更灵活 ④代码更加简洁纯粹的函数式编程,变量具有不可变性,同一个参数不会在不同场景下得出不同的结果,因此大大增强了系统的稳 ...

  3. Web后端 JAVA实现验证码生成与验证功能

    首先,写一个验证码生成帮助类,用来绘制随机字母: <span style="font-size:14px;">import java.awt.Color;  impor ...

  4. zList一个块状链表算法可以申请和释放同种对象指针,对于大数据量比直接new少需要差不多一半内存

    zList是一个C++的块状内存链表,特点: 1.对于某种类别需要申请大量指针,zList是一个很好的帮手,它能比new少很多内存. 2.它对内存进行整体管理,可以将数据和文件快速互操作 3.和vec ...

  5. Ajax - Apache安装配置

    apache安装配置 1.安装wamp2.配置根路径3.默认的网站根路径是安装目录的www子目录,如果不想使用默认目录,可以自己配置.配置方式如下: --找到文件wamp\bin\apache\Apa ...

  6. (详细)华为Mate7 MT7-TL00的usb调试模式在哪里开启的步骤

    就在我们使用pc连接安卓手机的时候,如果手机没有开启usb调试模式,pc则不能够成功识别我们的手机,在一些情况下,我们使用的一些功能较好的工具好比之前我们使用的一个工具引号精灵,老版本就需要打开usb ...

  7. StackExchange.Redis中文使用文档

    StackExchange.Redis中文使用文档 Intro 最近想深入学习一些 Redis 相关的东西.于是看了看官方的项目 StackExchange,发现里面有一份文档,于是打算翻译成中文,方 ...

  8. mysql export mysqldump version mismatch upgrade or downgrade your local MySQL client programs

    I use MySQL Community Edition and I solved this problem today. goto https://dev.mysql.com/downloads/ ...

  9. Microsoft Store应用安装路径和应用推荐——如何用Linux命令操控windows

    本人是cnblog萌新,刚学编程不久的菜鸟,这是我的第一篇博客,请各位轻喷 Microsoft store安装路径: 相信很多人都跟我一样有强迫症,文件找不到安装目录就不舒服.一开始在系统盘找不到Wi ...

  10. 网络流 P2770 航空路线问题

    #include <cstdio> #include <cstdlib> #include <map> #include <queue> #includ ...