POJ 1117 Pairs of Integers
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 4133 | Accepted: 1062 |
Description
Input
Output
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的更多相关文章
- Pairs of Integers
Pairs of Integers You are to find all pairs of integers such that their sum is equal to the given in ...
- poj 2239 Selecting Courses (二分匹配)
Selecting Courses Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8316 Accepted: 3687 ...
- OpenJudge/Poj 1207 The 3n + 1 problem
1.链接地址: http://bailian.openjudge.cn/practice/1207/ http://poj.org/problem?id=1207 2.题目: 总时间限制: 1000m ...
- OpenJudge/Poj 1125 Stockbroker Grapevine
1.链接地址: http://poj.org/problem?id=1125 http://bailian.openjudge.cn/practice/1125 2.题目: Stockbroker G ...
- POJ 2718 Smallest Difference(最小差)
Smallest Difference(最小差) Time Limit: 1000MS Memory Limit: 65536K Description - 题目描述 Given a numb ...
- poj 2284 That Nice Euler Circuit 解题报告
That Nice Euler Circuit Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 1975 Accepted ...
- 【POJ】1523 SPF(割点)
http://poj.org/problem?id=1523 太弱... too weak.. 割点我都还要看书和看题解来写..果然是写不出么.. 割点就那样求,然后分量直接这个节点有多少子树就有子树 ...
- POJ 1125 Stockbroker Grapevine【floyd简单应用】
链接: http://poj.org/problem?id=1125 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...
- Poj 1125 Stockbroker Grapevine(Floyd算法求结点对的最短路径问题)
一.Description Stockbrokers are known to overreact to rumours. You have been contracted to develop a ...
随机推荐
- git解决冲突(rebase版)
当使用git rebase碰到冲突时, git rebase <Remote Branch>/<Your Branch> 信息如下: error: Failed to merg ...
- [linux]Error: failure: repodata/repomd.xml from fedora: [Errno 256] No more mirrors to try.
在使用fedora17 系统的yum源的时候出现了例如以下错误: Error: failure: repodata/repomd.xml from fedora: [Errno 256] No mor ...
- VOIP NAT穿越之SIP信令穿越
本文原创自 http://blog.csdn.net/voipmaker 转载注明出处. 本文是VOIP通信NAT系列专题的第三篇, 本文论述NAT对SIP协议穿越的影响.SIP协议是基于文本的,而 ...
- spark not contain
参考网址 http://stackoverflow.com/questions/33608526/is-there-a-way-to-filter-a-field-not-containing-som ...
- 网页CSS常用中英文字体收集
Windows的中文字体: 黑体:SimHei 宋体:SimSun 新宋体:NSimSun 仿宋:FangSong 楷体:KaiTi 仿宋_GB2312:FangSong_GB2312 楷体_GB23 ...
- VC6.0在win 8.1中的安装使用
http://blog.csdn.net/liups/article/details/14646663 一.首先是win8.1的安装 本人选择的是win 8.1简体中文专业N版,文件名: cn_win ...
- python字符串 分片索引
字符串是字符的有序集合,可以通过其位置来获得具体的元素.在python中,字符串中的字符是通过索引来提取的,索引从0开始. python可以取负值,表示从末尾提取,最后一个为-1,倒数第二个为-2,即 ...
- chm只看到目录,看不到内容解决办法
鼠标左键->属性->解除锁定->搞定!
- osgearth2.8关于RectangleNodeEditor编辑点不可见的问题
static_cast<SphereDragger*>(_llDragger)->setColor(osg::Vec4(0,0,1,0)); Alpha写成了0 应该写成 stati ...
- OSG3.4编译FFMPEG插件
0.加入你要读a.mp4,那个正确的写法是osg::Image* image = osgDB::readImageFile("a.mp4.ffmpeg"); 1.在github上下 ...