Codeforces Round #567 (Div. 2) B. Split a Number

11 篇文章 0 订阅
1 篇文章 0 订阅

time limit per test

2 seconds

memory limit per test

512 megabytes

input

standard input

output

standard output

Dima worked all day and wrote down on a long paper strip his favorite number n

consisting of l

digits. Unfortunately, the strip turned out to be so long that it didn't fit in the Dima's bookshelf.

To solve the issue, Dima decided to split the strip into two non-empty parts so that each of them contains a positive integer without leading zeros. After that he will compute the sum of the two integers and write it down on a new strip.

Dima wants the resulting integer to be as small as possible, because it increases the chances that the sum will fit it in the bookshelf. Help Dima decide what is the minimum sum he can obtain.

Input

The first line contains a single integer l

(2≤l≤100000

) — the length of the Dima's favorite number.

The second line contains the positive integer n

initially written on the strip: the Dima's favorite number.

The integer n

consists of exactly l

digits and it does not contain leading zeros. Dima guarantees, that there is at least one valid way to split the strip.

Output

Print a single integer — the smallest number Dima can obtain.

Examples

Input

Copy

(copy)

7
1234567

Output

Copy

(copy)

1801

Input

Copy

(copy)

3
101

Output

Copy

(copy)

11

Note

In the first example Dima can split the number 1234567

into integers 1234 and 567. Their sum is 1801

.

In the second example Dima can split the number 101

into integers 10 and 1. Their sum is 11

. Note that it is impossible to split the strip into "1" and "01" since the numbers can't start with zeros.


思路:这题要用大数加法,其实这也还好,但有特例,比如

5

11001

要输出1002

我一开始直接遍历从中间到后面的0,然后一直wa,后边我发现了,没时间改,我直接从中间到前后都遍历一次,然后输出小的

然后。。最后六秒极限过题!

 代码:

#include <cstdio>
#include<math.h>
#include <iostream>
#include <algorithm>
using namespace std;
#define ll long long
#include<stdio.h>
#include<string.h>
#define MAX 150000
int L, M;
int mark;
void add(char a[], char b[], char c[]){
	int len1 = strlen(a);
	int len2 = strlen(b);
	int i, j;
	int l = 0;
	L = 0;
	int temp;
	for(i = 0; i < len1/2; i++){
		temp = a[i];
		a[i] = a[len1-1-i];
		a[len1-1-i] = temp;
	}//把输入的两个大数都逆序排列,这个循环很常见,可以专门写一个函数。
	for(i = 0; i < len2/2; i++){
		temp = b[i];
		b[i] = b[len2-1-i];
		b[len2-1-i] = temp;
	}
	for(i = 0; i < len1 && i < len2; i++){
		M = (a[i] - '0' + b[i] - '0' + L) % 10;
		L = (a[i] - '0' + b[i] - '0' + L) / 10;
		c[i] = M + '0';
	}//M是代表这一位的数,L是代表这一位两数相加后的十位数,也就是进位数的大小
	if(i < len1){
		for( ; i < len1; i++){
		M = (a[i] - '0' + L) % 10;
		L = (a[i] - '0' + L) / 10;
		c[i] = M + '0';
		}
	}
	if(i < len2){
		for( ; i < len2; i++){
		M = (b[i] - '0' + L) % 10;
		L = (b[i] - '0' + L) / 10;
		c[i] = M + '0';
		}
	}
	while(L){
		c[i++] = L + '0';
		L /= 10;
	}//最后一位的进位不要忘记
	mark = i-1;
}

int len;char num1[188080],num2[188000],a[180000],sum[188000],sum2[188000];
int js(int q)
{
    for(int i=0;i<=q;i++)
    {
        //printf("a[%d]=%d a[%d]=%d\n",i,a[i],q,a[i+q]);
        if(a[i]>a[i+q]) return 0;
        else if(a[i]<a[i+q]) return 1;
    }
    return 0;
}
int bj(char a[],char b[])
{
    if(strlen(a)>strlen(b)) return 1;
    else if(strlen(a)<strlen(b)) return -1;
    for(int i=strlen(a)-1;i>=0;i--)
    {
        //printf("a[%d]=%c b[%d]=%c\n",i,a[i],i,b[i]);
        if(a[i]>b[i]) return 1;
        else if(a[i]<b[i]) return -1;
    }
    return 0;
}
main(){

	/*scanf("%s%s",&num1,&num2);
	len=add(num1,num2,sum);
	for(i=len-1;i>=0;i--)
	printf("%d",sum[i]);
	printf("\n");*/
	int n;
	scanf("%d",&n);
	scanf("%s",a);
	int q=n/2,j=0;
	//printf("q.1=%d",q)
	while(a[q]=='0'&&q<n) q++;
	if(q==n/2&&(n%2==1))
    {
        if(js(q)==1)
        {
            if(a[q+1]!='0') q++;
        }
        //printf("q=%d\n",q);
    }
	for(int i=0;i<q;i++)
        num1[i]=a[i];
        //printf("num1=%s\n",num1);
    for(int i=q;i<n;i++)
        num2[j++]=a[i];
        //printf("num2=%s\n",num2);
    add(num1,num2,sum);

    memset(num1,0,sizeof(num1));
    memset(num2,0,sizeof(num2));
    q=n/2,j=0;
    while(a[q]=='0'&&q>=0) q--;
	if(q==n/2&&(n%2==1))
    {
        if(js(q)==1)
        {
            if(a[q+1]!='0') q++;
        }
        //printf("q=%d\n",q);
    }
	for(int i=0;i<q;i++)
        num1[i]=a[i];
        //printf("num1=%s\n",num1);
    for(int i=q;i<n;i++)
        num2[j++]=a[i];
        //printf("num2=%s\n",num2);
    add(num1,num2,sum2);

    if(bj(sum,sum2)<0)
    {
        //printf("1\n");
        for(int i=strlen(sum)-1;i>=0;i--)
            printf("%c",sum[i]);
            printf("\n");
    }
    else
    {
        for(int i=strlen(sum2)-1;i>=0;i--)
            printf("%c",sum2[i]);
            printf("\n");
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值