A. Omkar and Password

本文介绍了一种通过特定操作缩短数组长度的方法,即选择不相等的相邻元素并将其替换为它们的和,直至无法继续操作。文章提供了示例及解决方案。

A. Omkar and Password

time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Lord Omkar has permitted you to enter the Holy Church of Omkar! To test your worthiness, Omkar gives you a password which you must interpret!

A password is an array a of n positive integers. You apply the following operation to the array: pick any two adjacent numbers that are not equal to each other and replace them with their sum. Formally, choose an index i such that 1≤i<n and ai≠ai+1, delete both ai and ai+1 from the array and put ai+ai+1 in their place.

For example, for array [7,4,3,7] you can choose i=2 and the array will become [7,4+3,7]=[7,7,7]. Note that in this array you can’t apply this operation anymore.

Notice that one operation will decrease the size of the password by 1. What is the shortest possible length of the password after some number (possibly 0) of operations?

Input

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

The first line of each test case contains an integer n (1≤n≤2⋅105) — the length of the password.

The second line of each test case contains n integers a1,a2,…,an (1≤ai≤109) — the initial contents of your password.

The sum of n over all test cases will not exceed 2⋅105.

Output

For each password, print one integer: the shortest possible length of the password after some number of operations.

Example

input
2
4
2 1 3 1
2
420 420

output
1
2

My Answer Code:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin>>t;
    vector<int>num(t);
    vector<int>ans(t);
    for(int i=0;i<t;i++)
    {
        int flag=0;
        cin>>num[i];
        vector<int>a(num[i]);
        for(int j=0;j<num[i];j++)cin>>a[j];
        for(int k=0;k<num[i]-1;k++)
        {
            if(a[k]!=a[k+1]){flag=1;break;}
        }
        if(flag)ans[i]=1;
        else ans[i]=num[i];
    }
    for(int i=0;i<t;i++)cout<<ans[i]<<'\n';
    return 0;
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值