D. Finals in arithmetic

题目连接:

http://www.codeforces.com/contest/625/problem/D

Description

Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder.

Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13.

Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a.

As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist.

Input

The first line of the input contains a single integer n (1 ≤ n ≤ 10100 000).

Output

If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any.

Sample Input

4

Sample Output

2

Hint

题意

给你一个数k,要求你找到一个数x,使得x+反着的x = k

比如33,你就可以找21,因为21+12=33

不允许前导0

题解:

贪心,我们先不管前导0这个条件,我们如果sum[i]==sum[n-i-1]的话,ans[i]=(sum[i]+1)/2,ans[n-i-1]=sum[i]/2就好了

如果不相等的话,我们应该怎么呢?我们需要考虑进位

要么从后一位进1,要么从前一位退10回来,就这两种,讨论一下就好了

注意165这种数据,1开头的

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e6+7;
char s[maxn];
char ans[maxn];
int sum[maxn];
int n;
int check()
{
for(int i=0;i<n/2;)
{ int l=i,r=n-1-i;
if(sum[l]==sum[r])
i++;
else if(sum[l]==sum[r]+1||sum[l]==sum[r]+11)//考虑从后面进了一位,11 = 1+10
{
sum[l]--;
sum[l+1]+=10;
}
else if(sum[l]==sum[r]+10)//考虑从R前面退一位
{
sum[r-1]--;
sum[r]+=10;
}
else return 0;
}
if(n%2==1)
{
if(sum[n/2]%2==1||sum[n/2]>18||sum[n/2]<0)return 0;
ans[n/2]=sum[n/2]/2+'0';
}
for(int i=0;i<n/2;i++)
{
if(sum[i]>18||sum[i]<0)return 0;
ans[i]=(sum[i]+1)/2+'0';
ans[n-i-1]=(sum[i])/2+'0';
}
return ans[0]>'0';
}
int main()
{
scanf("%s",s);
n=strlen(s);
for(int i=0;i<n;i++)
sum[i]=s[i]-'0';
if(check())return puts(ans);
if(s[0]=='1'&&n>1)//首位为1的时候
{
for(int i=0;i<n;i++)
sum[i]=s[i+1]-'0';
sum[0]+=10;
n--;
if(check())puts(ans);
else printf("0");
}
else
printf("0");
}

Codeforces Round #342 (Div. 2) D. Finals in arithmetic 贪心的更多相关文章

  1. Codeforces Round #342 (Div. 2) D. Finals in arithmetic(想法题/构造题)

    传送门 Description Vitya is studying in the third grade. During the last math lesson all the pupils wro ...

  2. Codeforces Round #297 (Div. 2)C. Ilya and Sticks 贪心

    Codeforces Round #297 (Div. 2)C. Ilya and Sticks Time Limit: 2 Sec  Memory Limit: 256 MBSubmit: xxx  ...

  3. Codeforces Round #342 (Div. 2)

    贪心 A - Guest From the Past 先买塑料和先买玻璃两者取最大值 #include <bits/stdc++.h> typedef long long ll; int ...

  4. Codeforces Round #342 (Div. 2) C. K-special Tables 构造

    C. K-special Tables 题目连接: http://www.codeforces.com/contest/625/problem/C Description People do many ...

  5. Codeforces Round #342 (Div. 2) B. War of the Corporations 贪心

    B. War of the Corporations 题目连接: http://www.codeforces.com/contest/625/problem/B Description A long ...

  6. Codeforces Round #342 (Div. 2) A - Guest From the Past 数学

    A. Guest From the Past 题目连接: http://www.codeforces.com/contest/625/problem/A Description Kolya Geras ...

  7. Codeforces Round #342 (Div. 2) E. Frog Fights set 模拟

    E. Frog Fights 题目连接: http://www.codeforces.com/contest/625/problem/E Description stap Bender recentl ...

  8. Codeforces Round #342 (Div. 2) C. K-special Tables(想法题)

    传送门 Description People do many crazy things to stand out in a crowd. Some of them dance, some learn ...

  9. Codeforces Round #342 (Div. 2) B. War of the Corporations(贪心)

    传送门 Description A long time ago, in a galaxy far far away two giant IT-corporations Pineapple and Go ...

随机推荐

  1. Modernizr.js入门指南(HTML5&CSS3浏览器兼容插件)

    HTML5 和 CSS3 的快速发展,给我们带来了极大的便利,比如从此再也不用花费大量的时间只是为了设计一个圆角的效果. 但是!我们不能像控制机器一样来控制所有的人都一夜之间升级到现代浏览器,因为那些 ...

  2. JavaScript 教程学习进度备忘

    书签:"JavaScript 课外书"即“JS 教程”底部的“马上开始学习 JavaScript 高级教程吧 !”链接跳过,它属于高级教程:另外跳过的内容有待跟进 ________ ...

  3. [转] 編程風格要素-The Elements of Programming Style 中文英文中英對照

    转自: http://www.loliman3000.com/tech/2fe33ce32906f0302412881.php 下面的程序風格規則提煉自Brian Kernighan和P. J. Pl ...

  4. 3D游戏调研

    坦克大战3D http://s5.3d.tank365.com/tank.do?timestamp=1377220437056 深渊 http://sy.xd.com/

  5. 版本控制:SVN中Branch/tag的使用 -摘自网络

    在SVN中Branch/tag在一个功能选项中,在使用中也往往产生混淆. 在实现上,branch和tag,对于svn都是使用copy实现的,所以他们在默认的权限上和一般的目录没有区别.至于何时用tag ...

  6. BOX2D测试

    ; ; Box2DTestLayer = cc.Layer.extend({ world:null, //GLESDebugDraw *m_debugDraw; ctor:function () { ...

  7. 新工程软连接到原来的工程的out目录后,可以直接编译模块

    P508B_App_old_developer/alps$ ln -s  ../../P508B_App/alps/out 连接后,第一次编译后要加分支 ./mk hedy89_we_jb2 mm p ...

  8. 基于Linux2.6内核的加密容器法保护文件方法

            本文出自 "李晨光原创技术博客" 博客,谢绝转载!

  9. android 运行 python

    Jython is an implementation of the Python programming language designed to run on the Java platform. ...

  10. 转】Eclipse在线安装SVN

    原博文出自于: http://www.cnblogs.com/xdp-gacl/p/4354199.html 感谢! 对于,搞大数据的博主我,svn是需要了解的,很多源码包! 一.SVN在线安装 下面 ...