2022/3/4codeforces div2

//cf上的题目复制过来,格式挺标准的,这一点不错//

A. Square Counting

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Luis has a sequence of n+1n+1 integers a1,a2,…,an+1a1,a2,…,an+1. For each i=1,2,…,n+1i=1,2,…,n+1 it is guaranteed that 0≤ai<n0≤ai<n, or ai=n2ai=n2. He has calculated the sum of all the elements of the sequence, and called this value ss.

Luis has lost his sequence, but he remembers the values of nn and ss. Can you find the number of elements in the sequence that are equal to n2n2?

We can show that the answer is unique under the given constraints.

Input

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤2⋅1041≤t≤2⋅104). Description of the test cases follows.

The only line of each test case contains two integers nn and ss (1≤n<1061≤n<106, 0≤s≤10180≤s≤1018). It is guaranteed that the value of ss is a valid sum for some sequence satisfying the above constraints.

Output

For each test case, print one integer — the number of elements in the sequence which are equal to n2n2.

Example

input

Copy

4
7 0
1 1
2 12
3 12

output

Copy

0
1
3
1

Note

In the first test case, we have s=0s=0 so all numbers are equal to 00 and there isn't any number equal to 4949.

In the second test case, we have s=1s=1. There are two possible sequences: [0,1][0,1] or [1,0][1,0]. In both cases, the number 11 appears just once.

In the third test case, we have s=12s=12, which is the maximum possible value of ss for this case. Thus, the number 44 appears 33 times in the sequence.

这个签到题,不说了。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
     ll t;
     cin>>t;
     for(ll i=1;i<=t;i++)
     {
         ll n,s;
         cin>>n>>s;
         cout<<s/(n*n)<<endl;
     }
    return 0;
}

B. Quality vs Quantity

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given a sequence of nn non-negative integers a1,a2,…,ana1,a2,…,an. Initially, all the elements of the sequence are unpainted. You can paint each number Red–––––Red_ or Blue¯¯¯¯¯¯¯¯¯¯¯Blue¯ (but not both), or leave it unpainted.

For a color cc, Count(c)Count(c) is the number of elements in the sequence painted with that color and Sum(c)Sum(c) is the sum of the elements in the sequence painted with that color.

For example, if the given sequence is [2,8,6,3,1][2,8,6,3,1] and it is painted this way: [2¯¯¯,8,6––,3¯¯¯,1][2¯,8,6_,3¯,1] (where 66 is painted red, 22 and 33 are painted blue, 11 and 88 are unpainted) then Sum(Red–––––)=6Sum(Red_)=6, Sum(Blue¯¯¯¯¯¯¯¯¯¯¯)=2+3=5Sum(Blue¯)=2+3=5, Count(Red–––––)=1Count(Red_)=1, and Count(Blue¯¯¯¯¯¯¯¯¯¯¯)=2Count(Blue¯)=2.

Determine if it is possible to paint the sequence so that Sum(Red–––––)>Sum(Blue¯¯¯¯¯¯¯¯¯¯¯)Sum(Red_)>Sum(Blue¯) and Count(Red–––––)<Count(Blue¯¯¯¯¯¯¯¯¯¯¯)Count(Red_)<Count(Blue¯).

Input

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

The first line of each test case contains an integer nn (3≤n≤2⋅1053≤n≤2⋅105) — the length of the given sequence.

The second line of each test case contains nn integers a1,a2,…,ana1,a2,…,an (0≤ai≤1090≤ai≤109) — the given sequence.

It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052⋅105.

Output

For each test case, print YES if it is possible to paint the given sequence satisfying the above requirements, and NO otherwise.

You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as a positive response).

Example

input

Copy

4
3
1 2 3
5
2 8 6 3 1
4
3 5 4 2
5
1000000000 1000000000 1000000000 1000000000 1000000000

output

Copy

NO
YES
NO
NO

Note

In the first test case, there is no possible way to paint the sequence. For example, if you paint the sequence this way: [1¯¯¯,2¯¯¯,3––][1¯,2¯,3_] (where 33 is painted red, 11 and 22 are painted blue) then Count(Red–––––)=1<Count(Blue¯¯¯¯¯¯¯¯¯¯¯)=2Count(Red_)=1<Count(Blue¯)=2, but Sum(Red–––––)=3≯Sum(Blue¯¯¯¯¯¯¯¯¯¯¯)=3Sum(Red_)=3≯Sum(Blue¯)=3. So, this is not a possible way to paint the sequence.

In the second test case, a possible way to paint the sequence is described in the statement. We can see that Sum(Red–––––)=6>Sum(Blue¯¯¯¯¯¯¯¯¯¯¯)=5Sum(Red_)=6>Sum(Blue¯)=5 and Count(Red–––––)=1<Count(Blue¯¯¯¯¯¯¯¯¯¯¯)=2Count(Red_)=1<Count(Blue¯)=2.

In the third test case, there is no possible way to paint the sequence. For example, if you paint the sequence this way: [3––,5––,4¯¯¯,2¯¯¯][3_,5_,4¯,2¯] (where 33 and 55 are painted red, 44 and 22 are painted blue) then Sum(Red–––––)=8>Sum(Blue¯¯¯¯¯¯¯¯¯¯¯)=6Sum(Red_)=8>Sum(Blue¯)=6 but Count(Red–––––)=2≮Count(Blue¯¯¯¯¯¯¯¯¯¯¯)=2Count(Red_)=2≮Count(Blue¯)=2. So, this is not a possible way to paint the sequence.

In the fourth test case, it can be proven that there is no possible way to paint the sequence satisfying sum and count constraints.

这题,好像格式不行,看懂就可以,made第一次我以为最小的两个和最大的1个就行,样例误导我

后来发现 13 13 13 17 23 不是可以的吗。。。。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll a[200005];
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    ll t;
    scanf("%lld",&t);
    for(ll i=1; i<=t; i++)
    {
        ll n;
        scanf("%lld",&n);
        for(ll j=1; j<=n; j++)
        {
            scanf("%lld",&a[j]);
        }
        sort(a+1,a+1+n);
        ll tot1=a[1],tot2=0;
        ll l=1,r=n+1;
        while(l+1<r-1)
        {
            l++;
            r--;
            tot1+=a[l];
            tot2+=a[r];
        }
        if(tot1<tot2)printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}

C. Factorials and Powers of Two

time limit per test

3 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

A number is called powerful if it is a power of two or a factorial. In other words, the number mm is powerful if there exists a non-negative integer dd such that m=2dm=2d or m=d!m=d!, where d!=1⋅2⋅…⋅dd!=1⋅2⋅…⋅d (in particular, 0!=10!=1). For example 11, 44, and 66 are powerful numbers, because 1=1!1=1!, 4=224=22, and 6=3!6=3! but 77, 1010, or 1818 are not.

You are given a positive integer nn. Find the minimum number kk such that nn can be represented as the sum of kk distinct powerful numbers, or say that there is no such kk.

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.

A test case consists of only one line, containing one integer nn (1≤n≤10121≤n≤1012).

Output

For each test case print the answer on a separate line.

If nn can not be represented as the sum of distinct powerful numbers, print −1−1.

Otherwise, print a single positive integer  — the minimum possible value of kk.

Example

input

Copy

4
7
11
240
17179869184

output

Copy

2
3
4
1

Note

In the first test case, 77 can be represented as 7=1+67=1+6, where 11 and 66 are powerful numbers. Because 77 is not a powerful number, we know that the minimum possible value of kk in this case is k=2k=2.

In the second test case, a possible way to represent 1111 as the sum of three powerful numbers is 11=1+4+611=1+4+6. We can show that there is no way to represent 1111 as the sum of two or less powerful numbers.

In the third test case, 240240 can be represented as 240=24+32+64+120240=24+32+64+120. Observe that 240=120+120240=120+120 is not a valid representation, because the powerful numbers have to be distinct.

In the fourth test case, 17179869184=23417179869184=234, so 1717986918417179869184 is a powerful number and the minimum kk in this case is k=1k=1.

这题搜索吧,但是我位运算那边一直有问题,有几种方法对的,有一个不行,一直跑不动。。。

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int INF = 0x3f3f3f3f;
int T;
LL a, b;
LL w[111];
int cnt, ans;
bool flag;

LL count_one_bits(LL n)
{
	LL count=0;
	while(n)
	{
		count++;

		n=n&(n-1);
	}
	return count;
}

void dfs(int u,int use,LL now)
{
    if(u==cnt)
    {
        LL tmp=a-now;
        int s=use;
        s+=count_one_bits(tmp);
        ans=min(ans,s);
    }
    if(u>cnt) return ;
    dfs(u+1,1+use,w[u+1]+now);
    dfs(u+1,use,now);
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> T;
    a = 1, b = 2;
    while(a>0&&a<=1e12)
    {
        a*=b++;
        w[++cnt]=a;
    }
    while (T--)
    {
        cin >> a;
        ans = INF;
        dfs(0,0,0);
        cout << ans << endl;
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值