2023.2.10(总结)

昨天晚上打了一场cf的div2,现做出总结:
 

A. One and Two

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given a sequence a1,a2,…,ana1,a2,…,an. Each element of aa is 11 or 22.

Find out if an integer kk exists so that the following conditions are met.

  • 1≤k≤n−11≤k≤n−1, and
  • a1⋅a2⋅…⋅ak=ak+1⋅ak+2⋅…⋅ana1⋅a2⋅…⋅ak=ak+1⋅ak+2⋅…⋅an.

If there exist multiple kk that satisfy the given condition, print the smallest.

Input

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1001≤t≤100). Description of the test cases follows.

The first line of each test case contains one integer nn (2≤n≤10002≤n≤1000).

The second line of each test case contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤21≤ai≤2).

Output

For each test case, if there is no such kk, print −1−1.

Otherwise, print the smallest possible kk.

Example

input

Copy

 

3

6

2 2 1 2 1 2

3

1 2 1

4

1 1 1 1

output

Copy

2
-1
1

Note

For the first test case, k=2k=2 satisfies the condition since a1⋅a2=a3⋅a4⋅a5⋅a6=4a1⋅a2=a3⋅a4⋅a5⋅a6=4. k=3k=3 also satisfies the given condition, but the smallest should be printed.

For the second test case, there is no kk that satisfies a1⋅a2⋅…⋅ak=ak+1⋅ak+2⋅…⋅ana1⋅a2⋅…⋅ak=ak+1⋅ak+2⋅…⋅an

For the third test case, k=1k=1, 22, and 33 satisfy the given condition, so the answer is 11.

分析:

1,最后的乘积只和2有关系,所以先统计出2的个数,然后除以2,找到一半数的2的终止位置就行。

2,这不是给难题,要把题目读明白。

代码如下:

#include<stdio.h>
#include<math.h>
#include<string.h>
int t,n,a[1005];
int main()
{
    scanf("%d",&t);
    for(int i=0;i<t;i++)
    {
        int sum=0;
        scanf("%d",&n);
        for(int j=0;j<n;j++)
        {scanf("%d",a+j);
        if(a[j]==2)
        sum++;
        }
        int k=0;
        if(sum%2==1)
        {printf("-1\n");continue;}
        int j;
        for(j=0;j<n;j++)
        {
            if(a[j]==2)
            k++;
            if(k==sum/2)
            break;
        }
        printf("%d\n",j+1);
    }
} 

B. Sum of Two Numbers

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

The sum of digits of a non-negative integer aa is the result of summing up its digits together when written in the decimal system. For example, the sum of digits of 123123 is 66 and the sum of digits of 1010 is 11. In a formal way, the sum of digits of a=∑i=0∞ai⋅10ia=∑i=0∞ai⋅10i, where 0≤ai≤90≤ai≤9, is defined as ∑i=0∞ai∑i=0∞ai.

Given an integer nn, find two non-negative integers xx and yy which satisfy the following conditions.

  • x+y=nx+y=n, and
  • the sum of digits of xx and the sum of digits of yy differ by at most 11.

It can be shown that such xx and yy always exist.

Input

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤100001≤t≤10000).

Each test case consists of a single integer nn (1≤n≤1091≤n≤109)

Output

For each test case, print two integers xx and yy.

If there are multiple answers, print any.

Example

input

Copy

5
1
161
67
1206
19

output

Copy

1 0
67 94
60 7
1138 68
14 5

Note

In the second test case, the sum of digits of 6767 and the sum of digits of 9494 are both 1313.

In the third test case, the sum of digits of 6060 is 66, and the sum of digits of 77 is 77.

分析:

1,这个分为两个大块判断,也就是奇偶分判,对于偶数,直接除以2就行,得出来两个一模一样的数。

2,对于奇数,要特判最后一位是9的奇数,对于最后一位是9的奇数,我们采取一位一位判断的手法,一直保证我得出来的数要么和另外一个数相等,要么就大一,比如:249=124+125,199=145+54.

代码如下:

#include<stdio.h>
#include<math.h>
#include<string.h>
int main()
{
    int t;
    scanf("%d",&t);
    for(int i=0;i<t;i++)
    {
        int n;
        scanf("%d",&n);
        if(n%2==0)
        printf("%d %d\n",n/2,n/2);
        else if(n%10!=9)
        printf("%d %d\n",n/2+1,n/2);
        else 
        {
                int a[10]={0},b[10]={0};
                int j;
                int m=n;
                for(j=0;m>0;j++)
                {
                    a[j]=m%10;
                    m=m/10;
                }
                int k=j;
                int f=1;
                for(j=0;j<k;j++)
                {
                    if(a[j]%2==0)
                    {
                        b[j]=a[j]/2;
                        continue;
                    }
                    b[j]=a[j]/2+f;
                    if(f==1)
                    f=0;
                    else 
                    f=1;
                }
                int x=0;
                if(a[k-1]==1&&b[k-2]<a[k-2]-b[k-2])
                {x=1;k--;}
                for(j=k-1;j>=0;j--)
                {
                    x=x*10+b[j];
                }
                printf("%d %d\n",x,n-x);
        }
    }
}

总结:

题目读明白,心静下来写。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值