【SEERC2018A】【XSY3319】Numbers
给你一个数 \(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}\)。构造的方程为:
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的更多相关文章
- 【BZOJ1662】[Usaco2006 Nov]Round Numbers 圆环数 数位DP
[BZOJ1662][Usaco2006 Nov]Round Numbers 圆环数 Description 正如你所知,奶牛们没有手指以至于不能玩"石头剪刀布"来任意地决定例如谁 ...
- 【LeetCode-面试算法经典-Java实现】【129-Sum Root to Leaf Numbers(全部根到叶子结点组组成的数字相加)】
[129-Sum Root to Leaf Numbers(全部根到叶子结点组组成的数字相加)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a bina ...
- 【数位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 ...
- 【LeetCode】165. Compare Version Numbers 解题报告(Python)
[LeetCode]165. Compare Version Numbers 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...
- UVa 11582 Colossal Fibonacci Numbers! 【大数幂取模】
题目链接:Uva 11582 [vjudge] watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fil ...
- Python之路【第十八篇】:Web框架们
Python之路[第十八篇]:Web框架们 Python的WEB框架 Bottle Bottle是一个快速.简洁.轻量级的基于WSIG的微型Web框架,此框架只由一个 .py 文件,除了Pytho ...
- 【DFS深搜初步】HDOJ-2952 Counting Sheep、NYOJ-27 水池数目
[题目链接:HDOJ-2952] Counting Sheep Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 ...
- 【POI2003/2004 stage I】
[原题在此] Let us consider a game on a rectangular board m x 1 consisting of m elementary squares number ...
- POJ 3414 Pots【bfs模拟倒水问题】
链接: http://poj.org/problem?id=3414 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22009#probl ...
- 【LeetCode题意分析&解答】40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
随机推荐
- 从官方文档去学习之FreeMarker
一.前言 上一篇 <从现在开始,试着学会用官方文档去学习一个技术框架>提倡大家多去从官方文档学习技术,没有讲到具体的实践,本篇就拿一个案例具体的说一说,就是FreeMarker,选择这个框 ...
- 【资料总结】| Deep Reinforcement Learning 深度强化学习
在机器学习中,我们经常会分类为有监督学习和无监督学习,但是尝尝会忽略一个重要的分支,强化学习.有监督学习和无监督学习非常好去区分,学习的目标,有无标签等都是区分标准.如果说监督学习的目标是预测,那么强 ...
- CMD命令讲解(一)SC
参考网站:https://technet.microsoft.com/en-us/library/bb490995.aspx 备注:网站内容是翻译得来,源网站在上面 SC 与服务控制器和已安装的服务进 ...
- Java_基础篇(杨辉三角)
对于刚刚学Java的同学来说,杨辉三角是一个很好的例子. 杨辉三角让初学者更好的理解数组的定义和更好地去运用数组,特别是二维数组. 除此之外,还让初学者更好的掌握嵌套语句的使用. 以下是我的杨辉三角J ...
- Java学习点滴——泛型
基于<Java编程思想>第四版 前言 虽然Java的泛型在语法上和C++相比是类似的,但在实现上两者是全然不同的. 语法 Java只需要一个<>就可定义泛型.在<> ...
- MySQL中SELECT语句简单使用
最近开始复习mysql,查漏补缺吧. 关于mysql 1.MySQL不区分大小写,但是在MySQL 4.1及之前的版本中,数据库名.表名.列名这些标识符默认是区分大小写的:在之后的版本中默认不区分大小 ...
- npm ERR! asyncWrite is not a function
1.背景:原先node是官网下载安装的,通过brew更新了下,然后到项目里npm i 安装包时候,报错2.解决:卸载官网下载安装的node,重装 sudo rm -rf /usr/local/{bin ...
- Log4j配置(xml和property两种)
本文章为转载记录,纯粹个人备忘. 一.log4j.properties 的使用详解 1.输出级别的种类 ERROR.WARN.INFO.DEBUGERROR 为严重错误 主要是程序的错误WARN 为一 ...
- Activi相关表归纳
Activi相关归纳总结记录: ACT_RE_* : 'RE'表示repository.这个前缀的表包含了流程定义和流程静态资源(图片,规则,等等). ACT_RU_* : 'RU'表示 ...
- Android串口开发
参考资料: https://www.jianshu.com/p/9249ed03e745 GitHUb地址: https://github.com/AIlll/AndroidSerialPort An ...