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. js闭包用法

    闭包 既保证了 内部函数的私有性 又可以向外公开 通过一个已有对象 向它注入属性 /** * 闭包 * 在函数中定义的函数,在外部使用 * 1.在函数内部定义的函数,在外部不能访问 */ functi ...

  2. (转载)OC学习篇之---Foundation框架中的其他类(NSNumber,NSDate,NSExcetion)

    前一篇说到了Foundation框架中的NSDirctionary类,这一一篇来看一下Foundation的其他常用的类:NSNumber,NSDate,NSException. 注:其实按照Java ...

  3. 转 毛笔字教程ps

    跟大家分享一下毛笔字怎么做出来的,主要通过字体和素材叠加,十分简单,喜欢的一起练习.做完记得交作业. 先看看最终效果: 在网上是不是经常看这些碉堡了的毛笔感觉是不是很羡慕啊,现在我就教大家怎么做出这样 ...

  4. hadoop的ganglia数据监控

    如果我们想知道当前运行的hadoop集群的状态,可以通过hadoop的客户端和web页面来获得,但是如果我们想知道当前集群的繁忙程度,如读写次数,这些工具就办不到了.幸运的是hadoop提供了一种ga ...

  5. Eralng 小知识点

    文件属性 提取方法:Module:module_info/1 头文件 包含头文件 -include(FileName). %% FileName为绝对路径或相对路径 引入库中包含文件 -include ...

  6. Hadoop系列(二)hadoop2.2.0伪分布式安装

    一.环境配置 安装虚拟机vmware,并在该虚拟机机中安装CentOS 6.4: 修改hostname(修改配置文件/etc/sysconfig/network中的HOSTNAME=hadoop),修 ...

  7. windows分屏

    一.准备 主机.显示屏A.显示屏B.DVI连接线2根 二.操作步骤 1.使用DVI连接线将显示屏A连接到主机上,开机进入windows系统(演示用的是win 7)(若已连接,请跳到第2步.基本上这一步 ...

  8. Android通过tcpdump抓包

    1. 手机要有root权限 2. 下载tcpdump   http://www.strazzere.com/android/tcpdump 3. adb push c:\wherever_you_pu ...

  9. ubuntu下设置开机自动挂载硬盘

    我们在linux中常常用mount命令把硬盘分区或者光盘挂载到文件系统中./etc/fstab就是在开机引导的时候自动挂载到linux的文件系统. 如果给计算机配了一块新磁盘,已经分区,格式化,挂载, ...

  10. MacTerminal快捷键

    [MacTerminal快捷键] 在Mac系统中并没有Home.End等键,所以在使用时并不是特别的顺手,但是有几个键位组合可以使Terminal的操作更加灵活方便. 1.将光标移动到行首:ctrl ...