给你一个数 \(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}})\)

代码

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<ctime>
#include<utility>
#include<functional>
#include<cmath>
#include<vector>
#include<assert.h>
//using namespace std;
using std::min;
using std::max;
using std::swap;
using std::sort;
using std::reverse;
using std::random_shuffle;
using std::lower_bound;
using std::upper_bound;
using std::unique;
using std::vector;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef std::pair<int,int> pii;
typedef std::pair<ll,ll> pll;
void open(const char *s){
#ifndef ONLINE_JUDGE
char str[100];sprintf(str,"%s.in",s);freopen(str,"r",stdin);sprintf(str,"%s.out",s);freopen(str,"w",stdout);
#endif
}
void open2(const char *s){
#ifdef DEBUG
char str[100];sprintf(str,"%s.in",s);freopen(str,"r",stdin);sprintf(str,"%s.out",s);freopen(str,"w",stdout);
#endif
}
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;}
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');}
int upmin(int &a,int b){if(b<a){a=b;return 1;}return 0;}
int upmax(int &a,int b){if(b>a){a=b;return 1;}return 0;}
ll ans;
int t,t2;
int a[100];
int b[100];
ll n;
void gao1()
{
for(int i=1;i<=t2;i++)
if(b[i]!=b[t2-i+1])
return;
ll res=1;
for(int i=1;i<=(t2+1)/2;i++)
res*=min(9,(i==1?b[i]-1:b[i]))-max(b[i]-9,(i==1?1:0))+1;
ans+=res;
}
int a1[100],a2[100];
void gao2()
{
for(int i=1;i<t2;i++)
{
for(int j=1;j<=t2;j++)
a1[j]=a2[j]=0;
for(int j=t2;j>i;j--)
if(!a1[j])
{
int x=j;
int v=b[j];
while(!a1[x])
{
a1[x]=v;
x=t2-x+1;
a1[x]=v;
if(a2[x])
break;
if(x>i)
break;
a2[x]=v=b[x]-a1[x];
x=i-x+1;
a2[x]=v;
v=b[x]-a2[x];
}
}
int flag=1;
for(int j=1;flag&&j<=t2;j++)
if(a1[j]<0||a1[j]>9||a1[j]+a2[j]!=b[j]||a1[t2-j+1]!=a1[j])
flag=0;
for(int j=1;flag&&j<=i;j++)
if(a2[j]<0||a2[j]>9||a2[j]!=a2[i-j+1])
flag=0;
if(a2[i]==0)
flag=0;
if(flag)
ans+=2;
}
}
int main()
{
open("number");
scanf("%lld",&n);
ll m=n;
while(m)
{
a[++t]=m%10;
m/=10;
}
for(int i=0;i<1<<(t-1);i++)
{
for(int j=1;j<=t;j++)
b[j]=a[j];
for(int j=1;j<=t;j++)
if((i>>(j-1))&1)
{
b[j]+=10;
b[j+1]--;
}
int flag=1;
for(int j=1;j<=t;j++)
if(b[j]<0||b[j]>18)
{
flag=0;
break;
}
if(!flag)
continue;
t2=t;
while(!b[t2])
t2--;
gao1();
gao2();
}
printf("%lld\n",ans);
return 0;
}

【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. docker daemon 配置文件

    Ubuntu Ubuntu 14.04 配置文件位于 /etc/init/docker.conf Ubuntu 15.04 配置文件位于 /etc/default/docker,修改配置项DOCKER ...

  2. 《全栈营销之如何制作个人博客》之一:用什么开发语言和CMS系统

    现在的互联网,已经不再是初级互联时代,是需要全方位营销,自建粉丝池的时代,云码素材从提出全栈营销,到实践分析,总结出全栈营销第一步,先制作一个个人博客是最好的开始.今天开始就跟我一起学习如何制作一个精 ...

  3. vue 日期时间过滤器

    来自:https://blog.csdn.net/m0_37068028/article/details/72898154 侵删 来自:https://segmentfault.com/a/11900 ...

  4. 一种动态写入apk数据的方法(用于用户关系绑定、添加渠道号等)

    背景: 正在开发的APP需要记录业务员与客户的绑定关系.具体应用场景如下: 由流程图可知,并没有用户填写业务人员信息这一步,因此在用户下载的APP中就已经携带了业务人员的信息. 由于业务人员众多,不可 ...

  5. node.js解析微信消息推送xml格式加密的消息

    之前写过一个解密json格式加密的,我以为xml的和json的差不多,是上上个星期五吧,我的同事也是在做微信公众号里面的消息推送解密,发现好像只能使用xml加密格式的发送到服务器,我们去年也做过企业微 ...

  6. java倒计时三种简单实现方式

    写完js倒计时,突然想用java实现倒计时,写了三种实现方式 一:设置时长的倒计时: 二:设置时间戳的倒计时: 三:使用java.util.Timer类实现的时间戳倒计时 代码如下: package ...

  7. LeetCode算法题-Find Pivot Index(Java实现)

    这是悦乐书的第304次更新,第323篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第172题(顺位题号是724).给定一个整数nums数组,编写一个返回此数组的" ...

  8. 【导航】FPGA相关

    [博客索引] FPGA相关 数字电路实现上,较多的经验是基于Xilinx/Altera的FPGA,使用Verilog语言,实现光传输SDH.OTN通信协议,DDR3控制器应用,以及视频分割.合并.sc ...

  9. Spring第一天——入门与IOC

    大致内容 spring基本概念 IOC入门 [17.6.9更新],如何学习spring? 掌握用法 深入理解 不断实践 反复总结 再次深入理解与实践 一.Spring相关概念  1.概述: Sprin ...

  10. 19 款仿 Bootstrap 后台管理主题免费下载

    声明: 1. 本篇文章提到的仿 Bootstrap 风格的主题,是基于 jQuery 的 ASP.NET MVC 控件库的主题. 2. FineUIMvc(基础版)完全免费,可以用于商业项目. 目录 ...