Pairs of Integers
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 4133   Accepted: 1062

Description

You are to find all pairs of integers such that their sum is equal to the given integer number N and the second number results from the first one by striking out one of its digits. The first integer always has at least two digits and starts with a non-zero digit. The second integer always has one digit less than the first integer and may start with a zero digit.

Input

The input file consists of a single integer N (10 <= N <= 10^9).

Output

On the first line of the output file write the total number of different pairs of integers that satisfy the problem statement. On the following lines write all those pairs. Write one pair on a line in ascending order of the first integer in the pair. Each pair must be written in the following format:

X + Y = N

Here X, Y, and N, must be replaced with the corresponding integer numbers.
There should be exactly one space on both sides of '+' and '=' characters.

Sample Input

302

Sample Output

5
251 + 51 = 302
275 + 27 = 302
276 + 26 = 302
281 + 21 = 302
301 + 01 = 302

Source

 

【题意】

给出一个数N,求X+Y = N的所有数对(X,Y),X,Y有如下要求,Y是X这个数删除一位所得到的数,X不能含有前导0,但是Y可以含有前导0.

【分析】

将数分三段,可以把X看成三部分:HSL,高位H,低位L,中间被strike掉的位S

所以Y 就是HL

X = (H*10 + S)*10^i + L, (i = 0, 1, 2 ... 最多log10(N),i代表L是几位数)

Y = H*10^i + L

X + Y = (H*11 + S) * 10^i + 2*L = N

N/(10^i) = (11H+S) + 2L/(10^i),其中2L/(10^i)只可能为0和1,再加上i的不到10种取值,共20种不到的组合
 

所以我们通过枚举L的值,来推导出H和S。

当N是奇数的时候,只可能是删除X的最后一位得到,(原因是如果L存在,则2*L%(10^i)取余

是N的后i位,因为2*L是偶数,所以N必然四偶数)。此时变成H*11 + S = N

由于S是一个数字,其值只能是0~9,故当N%11 != 10的时候是有解的。

1.N是奇数,N%11 != 10,有一个解。

2.N是偶数,还是需要考虑删除的是最后一位的情况,该情形和奇数的是一样的。

3.当枚举L的时候,又分为两种情况,2*L有进位,和2*L无进位,

即(2*L)%(10^i) = N%(10^i)

举个例子吧:

假设L是一位数,发现N的末尾是2,

则我们可以猜测的是,L = 1, 2*L = 2, 2*L无进位

然而L = 6,也是满足条件的,2*L = 12, 2*L%(10^i) = 2,即2*L向前进了1,其余数为2.

 

最后这样求解还可能存在重复的结果,所以我们map去一下重

 


【代码】

#include<map>
#include<cstdio>
#include<iomanip>
#include<iostream>
using namespace std;
int n,H,S,L,X;map<int,int>res,ri;
int main(){
cin>>n;
for(int i=0,I=1;I<=n;i++,I*=10){
if(n%I%2) continue;
H=n/I/11;
S=n/I%11;
L=n%I/2;
if(S<=9){
X=(H*10+S)*I+L;
if(H+S) res[X]=H*I+L;
ri[X]=i;
}
L=(n%I+I)/2;
S=n/I%11-1;
if(S>=0&&L){
X=(H*10+S)*I+L;
if(H+S) res[X]=H*I+L;
ri[X]=i;
}
}
cout<<res.size()<<endl;
for(map<int,int>::iterator it=res.begin();it!=res.end();it++)
cout<<it->first<<" + "<<setw(ri[it->first])<<setfill('0')<<it->second<<" = "<<n<<endl;
return 0;
}
 
 

 

POJ 1117 Pairs of Integers的更多相关文章

  1. Pairs of Integers

    Pairs of Integers You are to find all pairs of integers such that their sum is equal to the given in ...

  2. poj 2239 Selecting Courses (二分匹配)

    Selecting Courses Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8316   Accepted: 3687 ...

  3. OpenJudge/Poj 1207 The 3n + 1 problem

    1.链接地址: http://bailian.openjudge.cn/practice/1207/ http://poj.org/problem?id=1207 2.题目: 总时间限制: 1000m ...

  4. OpenJudge/Poj 1125 Stockbroker Grapevine

    1.链接地址: http://poj.org/problem?id=1125 http://bailian.openjudge.cn/practice/1125 2.题目: Stockbroker G ...

  5. POJ 2718 Smallest Difference(最小差)

     Smallest Difference(最小差) Time Limit: 1000MS    Memory Limit: 65536K Description - 题目描述 Given a numb ...

  6. poj 2284 That Nice Euler Circuit 解题报告

    That Nice Euler Circuit Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 1975   Accepted ...

  7. 【POJ】1523 SPF(割点)

    http://poj.org/problem?id=1523 太弱... too weak.. 割点我都还要看书和看题解来写..果然是写不出么.. 割点就那样求,然后分量直接这个节点有多少子树就有子树 ...

  8. POJ 1125 Stockbroker Grapevine【floyd简单应用】

    链接: http://poj.org/problem?id=1125 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...

  9. Poj 1125 Stockbroker Grapevine(Floyd算法求结点对的最短路径问题)

    一.Description Stockbrokers are known to overreact to rumours. You have been contracted to develop a ...

随机推荐

  1. 正則表達式re中的贪心算法和非贪心算法 在python中的应用

    之前写了一篇有关正則表達式的文章.主要是介绍了正則表達式中通配符 转义字符 字符集 选择符和子模式 可选项和反复子模式 字符串的開始和结尾 ,有兴趣的能够查看博客内容. 此文章主要内容将要介绍re中的 ...

  2. SqlServer2008基础知识:安全与权限

    分享自 儒雅的男人blog http://www.cnblogs.com/yushaoye201314/archive/2013/04/19/3031203.html 好文,转载收藏 这两天在调用Mi ...

  3. [Err] 1231 - Variable 'sql_mode' can't be set to the value of 'NULL

    在MYSQL还原语句的时候,报: [Err] - Variable 'sql_mode' can't be set to the value of 'NULL 解决办法:打开SQL语句,把里面的注释给 ...

  4. 源码分析五(HashSet的内部实现)

    一:首先来看看Hashset的继承体系 public class HashSet<E> extends AbstractSet<E> implements Set<E&g ...

  5. symfony window下的安装 安装时候出现的问题以及解决方案

    1. cmd进入DOS  , cd 到 php.exe 的目录下 2.         php -r "readfile('http://symfony.com/installer');&q ...

  6. [原]unity3D 相机跟随

    using UnityEngine;using System.Collections; public class CameraFollow : MonoBehaviour {            p ...

  7. messagpack的使用

    我打算使用messagepack对通信的对象进行序列化,使用的方式参考这篇文章: http://www.cppfans.org/1410.html 此处记录一下自己遇到的一些问题 先是用VS2010对 ...

  8. 如何查看MySQL的当前存储引擎?

    如何查看MySQL的当前存储引擎? 一般情况下,mysql会默认提供多种存储引擎,你可以通过下面的查看:   看你的mysql现在已提供什么存储引擎: mysql> show engines; ...

  9. 【权限维持】window服务端常见后门技术

    0x00 前言 未知攻焉知防,攻击者在获取服务器权限后,通常会用一些后门技术来维持服务器权限,服务器一旦被植入后门,攻击者如入无人之境.这里整理一些window服务端常见的后门技术,了解攻击者的常见后 ...

  10. RF失败案例重跑

    1.1        失败案例重跑 该功能主要是针对上次连跑失败的案例需要重新执行测试的情况,可自动识别上次执行失败的案例并进行重跑,无需手动选择相应的案例,简单高效. 1.5.1.        重 ...