给你一个数 \(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. WCF优雅使用 KnownType标记的方法

    [KnownType("DerivedTypes")] [DataContract] public abstract class TaskBase { // other class ...

  2. httpclient绕过证书验证进行HTTPS请求

    http请求是我们常用的一种web应用的应用层协议,但是由于它的不安全性,现在正在逐渐向https协议过渡.https协议是在http的基础上进行了隧道加密,加密方式有SSL和TLS两种.当serve ...

  3. JavaScript实现获取两个排序数组的中位数算法示例

    本文实例讲述了JavaScript排序代码实现获取两个排序数组的中位数算法.分享给大家供大家参考,具体如下: 题目 给定两个大小为 m 和 n 的有序数组 nums1 和 nums2 . 请找出这两个 ...

  4. webstorm 2018.2.3 cmd+w无法关闭文件

  5. 前端页面基于JQuery的点击事件

    一,使用id选择器 1.方式一 $("#id").click(function(){ do something }) 2.方式二 $("#id").on(&qu ...

  6. Odoo的模块和应用程序的区别和使用

    一.模块(modules)和应用程序(application)的区别: 模块元件是Odoo应用程序的组成快.模块可以将新功能添加到Odoo,或改变现有功能.模块是一个包含名为__manifest__. ...

  7. Admin Console 反应慢的相关bug

    一个常见问题是在 Admin console 刷新 server 列表时,页面反应慢.从 Admin Server 的 Thread Dump 可以看到 Admin server 到 Managed ...

  8. iOS---------获取当前年份

    NSDate *  senddate=[NSDate date]; NSDateFormatter  *dateformatter=[[NSDateFormatter alloc] init]; [d ...

  9. HandlerThread原理分析

    HandlerThread是一个内部拥有Handler和Looper的特殊Thread,可以方便地在子线程中处理消息. 简单使用 HandlerThread的使用比较简单. mHandlerThrea ...

  10. SQLServer之锁简介

    锁定义(Definition) 锁定是 DBMS 将访问限制为多用户环境中的行的过程. 以独占方式锁定行或列,不允许其他用户访问锁定的数据,直到锁被释放. 这可确保两个用户不能同时更新行中的同一列. ...