[ARC208 (Div. 2)B]B - Sum of Mod题解

Time Limit: 2 sec / Memory Limit: 1024 MiB

Score : 600 points

Problem Statement

You are given positive integers N and K.

Find one length-N sequence of positive integers A=(A1​,A2​,…,AN​) with the minimum value of AN​ among the ones that satisfy all of the following conditions.

  • A is non-decreasing. That is, Ai​≤Ai+1​ for 1≤i≤N−1.
  • i=1∑N−1​(Ai+1​modAi​)=K.

You are given T test cases; solve each of them.

有道 翻译

问题陈述

给定正整数 N 和 K 。

在满足下列所有条件的正整数序列中,求一个长度为- N 且最小值为 AN​ 的正整数序列 A=(A1​,A2​,…,AN​) 。

  • A 是非递减的。也就是说, 1≤i≤N−1 对应 Ai​≤Ai+1​ 。

—— i=1∑N−1​(Ai+1​modAi​)=K 。

给你 T 个测试用例;每个都解出来。

Constraints

  • 1≤T≤105
  • 2≤N≤2×105
  • The sum of N over all test cases is at most 2×105.
  • 1≤K≤109
  • All input values are integers.

有道 翻译

# #约束

—— 1≤T≤105

—— 2≤N≤2×105

—所有测试用例 N 的总和不超过 2×105 。

—— 1≤K≤109

—所有输入值均为整数。


Input

The input is given from Standard Input in the following format:

T
case1​
case2​
⋮
caseT​

Each test case is given in the following format:

N K

有道 翻译

# #输入

输入来自标准输入,格式如下:

T
case1​
case2​
⋮
caseT​

每个测试用例以以下格式给出:

N K

Output

Output the answers for each test case in order, separated by newlines.

For each test case, print A that satisfies all conditions and has the minimum value of AN​ in the following format:

A1​ A2​ … AN​

If there are multiple A that satisfy all conditions and have the minimum value of AN​, any of them will be accepted.

有道 翻译

# #输出

按顺序输出每个测试用例的答案,以换行符分隔。

对于每个测试用例,打印满足所有条件且最小值为 AN​ 的 A ,格式如下:

A1​ A2​ … AN​

如果有多个 A 满足所有条件且最小值为 AN​ ,则接受其中任何一个。


Sample Input 1

Copy

2
5 3
2 3

Sample Output 1

Copy

1 2 3 4 5
4 7

Consider the first test case.

A=(1,2,3,4,5) is non-decreasing and satisfies i=1∑N−1​(Ai+1​modAi​)=(2mod1)+(3mod2)+(4mod3)+(5mod4)=3=K, so it satisfies all conditions.

There does not exist an A that satisfies all conditions and has a value of AN​ less than 5, so printing A=(1,2,3,4,5) will be accepted.

Other than this, printing A=(2,3,4,5,5) or A=(2,3,3,5,5) will also be accepted.

有道 翻译

###输出示例

1 2 3 4 5
4 7

考虑第一个测试用例。

A=(1,2,3,4,5) 是非递减的,且满足 i=1∑N−1​(Ai+1​modAi​)=(2mod1)+(3mod2)+(4mod3)+(5mod4)=3=K ,因此满足所有条件。

不存在满足所有条件且值 AN​ 小于 5 的 A ,因此将接受打印 A=(1,2,3,4,5) 。

除此之外,打印 A=(2,3,4,5,5) 或 A=(2,3,3,5,5) 也可以接受。

思路

直接贪心。

代码见下

#include<bits/stdc++.h>
using namespace std;
long long t,n,k,a[200005],lk=0;
long long abc(long long a1){
	long long hj=2,op=0;
	for(int i=1;i<=n-1;i++){
		op+=a1%(a1/2+1);
		a1=a1/2+1;
		if(a1==1) break;
	}
	return op;
}
int main(){
    cin>>t;
    while(t--){
        cin>>n>>k;
        long long l=1,r=1e18+7,md=1e18+7;
        while(l<=r){
        	long long mid=(l+r)/2;
        	if(abc(mid)>=k){
        		md=min(md,mid);
        		r=mid-1;
			}
			else{
				l=mid+1;
			} 
		}
		a[n]=md;
		lk=0;
		for(int i=n;i>=2;i--){
			if(a[i]%(a[i]/2+1)+lk<=k){
				lk+=a[i]%(a[i]/2+1);
				a[i-1]=(a[i]/2+1);
			}
			else if(k==lk){
				for(int j=i-1;j>=1;j--){
					a[j]=1;
				}
				break;
			}
			else{
				a[i-1]=a[i]/2+a[i]%(a[i]/2+1)+lk-k;
				lk=k;
			}
		}
		for(int i=1;i<=n;i++){
			cout<<a[i]<<" ";
		}
		cout<<endl;
    }
    return 0;
}

posted @ 2025-10-13 10:52  bz02_2023f2  阅读(10)  评论(0)    收藏  举报  来源