给你一个数 \(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. 分布式缓存Hazelcast案例一

    分布式缓存Hazelcast案例一 Hazelcast IMDG Architecture 今天先到这儿,希望对您技术领导力, 企业管理,物联网,  系统架构设计与评估,团队管理, 项目管理, 产品管 ...

  2. 利用Redis keyspace notification(键空间通知)实现过期提醒

    一.序言: 本文所说的定时任务或者说计划任务并不是很多人想象中的那样,比如说每天凌晨三点自动运行起来跑一个脚本.这种都已经烂大街了,随便一个 Crontab 就能搞定了. 这里所说的定时任务可以说是计 ...

  3. Java 初始化a=2 打印a+++a++为5

    这段程序大概这样: public static void main(String[] args){ int a = 2; System.out.println(a+++a++); } 编译后的字节码为 ...

  4. MySQL事务及ACID特性

    一.事物 1.定义:事务是访问和更新数据库的程序执行单元,事务中包含一条或者多条SQL语句,这些语句要么全部执行成功,要么都不执行. 在MySQL中,事务支持是在引擎层实现的,MySQL是一个支持多引 ...

  5. Dotnetcore 开发速记

    1.System.InvalidOperationException:"Internal connection fatal error." 全球固定模式,坑爹 https://gi ...

  6. git 提交项目代码到码云步骤 以及出现错误解决办法

    git initgit remote add origin 项目地址git add .git commit -m "注释"git push origin master 出现错误 $ ...

  7. 七、Android动画

    Android的动画可以分为三种:View动画.帧动画和属性动画,帧动画也属于View动画的一种,只不过它和平移.旋转等常见的View动画在表现形式上略有不同而已. 1.View动画 平移动画:Tra ...

  8. Git的安装与配置

    在安装Git之前,首先要下载Git安装包. 下载地址:https://gitforwindows.org/ 下载完后打开安装:如下步骤                       按着以上步骤安装完成 ...

  9. Python第十三天 django 1.6 导入模板 定义数据模型 访问数据库 GET和POST方法 SimpleCMDB项目 urllib模块 urllib2模块 httplib模块 django和web服务器整合 wsgi模块 gunicorn模块

    Python第十三天   django 1.6   导入模板   定义数据模型   访问数据库   GET和POST方法    SimpleCMDB项目   urllib模块   urllib2模块 ...

  10. Python 小试牛刀,Django详细解读,让你更快的掌握它!!!

    一.MVC和MTV模式 MVC:将web应用分为模型(M),控制器(C),视图(V)三层:他们之间以一种插件似的,松耦合的方式连接在一起. 模型负责业务对象与数据库的对象(ORM),视图负责与用户的交 ...