【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 ...
随机推荐
- 上传图片截图预览控件不显示cropper.js 跨域问题
上传图片到图片服务器,因为域名不同,多以会有跨域问题. No 'Access-Control-Allow-Origin' header is present on the requested reso ...
- Java 8中Stream API学习笔记
1)函数式编程的优势和劣势分别是什么?优势:①不可变性 ②并行操作 ③执行顺序更灵活 ④代码更加简洁纯粹的函数式编程,变量具有不可变性,同一个参数不会在不同场景下得出不同的结果,因此大大增强了系统的稳 ...
- Web后端 JAVA实现验证码生成与验证功能
首先,写一个验证码生成帮助类,用来绘制随机字母: <span style="font-size:14px;">import java.awt.Color; impor ...
- zList一个块状链表算法可以申请和释放同种对象指针,对于大数据量比直接new少需要差不多一半内存
zList是一个C++的块状内存链表,特点: 1.对于某种类别需要申请大量指针,zList是一个很好的帮手,它能比new少很多内存. 2.它对内存进行整体管理,可以将数据和文件快速互操作 3.和vec ...
- Ajax - Apache安装配置
apache安装配置 1.安装wamp2.配置根路径3.默认的网站根路径是安装目录的www子目录,如果不想使用默认目录,可以自己配置.配置方式如下: --找到文件wamp\bin\apache\Apa ...
- (详细)华为Mate7 MT7-TL00的usb调试模式在哪里开启的步骤
就在我们使用pc连接安卓手机的时候,如果手机没有开启usb调试模式,pc则不能够成功识别我们的手机,在一些情况下,我们使用的一些功能较好的工具好比之前我们使用的一个工具引号精灵,老版本就需要打开usb ...
- StackExchange.Redis中文使用文档
StackExchange.Redis中文使用文档 Intro 最近想深入学习一些 Redis 相关的东西.于是看了看官方的项目 StackExchange,发现里面有一份文档,于是打算翻译成中文,方 ...
- 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/ ...
- Microsoft Store应用安装路径和应用推荐——如何用Linux命令操控windows
本人是cnblog萌新,刚学编程不久的菜鸟,这是我的第一篇博客,请各位轻喷 Microsoft store安装路径: 相信很多人都跟我一样有强迫症,文件找不到安装目录就不舒服.一开始在系统盘找不到Wi ...
- 网络流 P2770 航空路线问题
#include <cstdio> #include <cstdlib> #include <map> #include <queue> #includ ...