[AtCoderContest075F]Mirrored

试题描述

For a positive integer \(n\), we denote the integer obtained by reversing the decimal notation of n (without leading zeroes) by \(rev(n)\). For example, \(rev(123)=321\) and \(rev(4000)=4\).

You are given a positive integer \(D\). How many positive integers \(N\) satisfy \(rev(N)=N+D\)?

\(rev(n)\) 表示 \(n\) 在十进制表示下的倒过来写的数,对于给定的 \(D\),求有多少个正整数 \(N\) 满足 \(rev(N) = N + D\)。

输入

Input is given from Standard Input in the following format:

D

输出

Print the number of the positive integers \(N\) such that \(rev(N)=N+D\).

输入示例1

63

输出示例1

2

输入示例2

75

输出示例2

0

输入示例3

864197532

输出示例3

1920

数据规模及约定

\(D\) is an integer.

\(1 \le D < 10^9\)

题解

题目地址

考虑从两头向中间依次确定每一位,考虑每一位的贡献。

  abcdefg
- gfedcba

所以 \(a - g\) 的贡献是 \((a - g) * 999999\),下一位贡献是 \((b - f) * 999900\)……所以 \(D\) 必须是 \(9\) 的倍数。令 \(d = D \div 9\)

那么每一位的贡献依次是:

111111
11110
1100

上面是偶数位的情况,偶数位类似。

注意到上面的形式,我们可以根据 \(d\) 确定出每一位的数位差是多少。

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <algorithm>
using namespace std; int read() {
int x = 0, f = 1; char c = getchar();
while(!isdigit(c)){ if(c == '-') f = -1; c = getchar(); }
while(isdigit(c)){ x = x * 10 + c - '0'; c = getchar(); }
return x * f;
} #define maxn 15
#define LL long long int d;
LL ans; int main() {
d = read(); if(d % 9) return puts("0"), 0;
d /= 9;
// printf("d: %d\n", d);
LL ini = 0, ten = 1;
for(int i = 1; i <= 17; i++) {
LL tmp = 1, D = d;
ini = ini * 10 + 1;
LL base = ini, lten = ten, rten = 1;
for(int j = 0; j <= (i >> 1); j++) {
// printf("%lld * %lld [tmp *= %lld] (%lld)\n", base, D % (rten * 10) / rten, 10 - abs(D % (rten * 10) / rten) - (!j ? 1 : 0), D);
tmp *= 10 - abs(D % (rten * 10) / rten) - (!j ? 1 : 0);
D -= base * (D % (rten * 10) / rten);
base -= lten + rten;
lten /= 10; rten *= 10;
}
// printf("%lld * %d (%lld)\n", base, D % (rten * 10) / rten, D);
ten *= 10;
// printf("(%lld %lld)\n", ini, tmp);
if(!D) ans += tmp;
} printf("%lld\n", ans); return 0;
}

最新文章

  1. 高性能Mysql主从架构的复制原理及配置详解
  2. ie8用ajax访问不能每次都刷新的问题
  3. 关闭SSMS的事务自动提交,改为手动提交
  4. str()和repre()的区别
  5. ccs6.0使用问题记录
  6. PHP获取Cookie模拟登录
  7. json &lt;---&gt;List集合,实体类 之间的相互转换
  8. Nyoj 三国志(dijkstra+01背包)
  9. http://codeforces.com/contest/612/problem/D
  10. ArcGIS 网络分析[1.4] 制作点线要素时需要注意的地方
  11. L2-001. 紧急救援(PAT)~最短路应用
  12. @font-face使用在线字体
  13. Angular features and services overview
  14. 您无法登陆系统。原因可能是您的用户记录或所属的业务部门在Microoft Dynamics CRM中已被禁用
  15. IntelliJ IDEA 注释模板设置
  16. Android开发学习笔记-自定义组合控件
  17. Git的基本使用方法和安装&amp;心得体会(使用git命令行)
  18. ORA-01157:无法标识/锁定数据文件,ORA-01110:数据文件。。。
  19. Java应用开发中的SQL注入攻击
  20. Linux安装设置VNC远程桌面

热门文章

  1. 小常识:变量的修饰符和DEMO
  2. 解决ubuntu上ifconfig没有eth0/ens33且无法上网的问题
  3. Bootstrap历练实例:带列表组的面板
  4. NSURL初始化失败
  5. Codevs1081 线段树练习 2
  6. NOIP模拟赛 无线通讯网
  7. 转 救命的教程 anaconda下载安装包网络错误的解决办法
  8. Spring Boot 应用 快速发布到linux服务器的脚本代码示例
  9. 201621123080《java程序设计》第14周实验总结
  10. GoogleTest 之路2-Googletest 入门(Primer)