import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.ArrayList;
public class Main {
// Function to calculate the sum of an arithmetic sequence
static long findSum(long n, long a, long d) {
long result = n * (2 * a + (n - 1) * d);
result = result / 2;
return result;
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] data = br.readLine().split(" ");
int t = Integer.parseInt(data[0]); // Number of test cases
int index = 1;
ArrayList<String> results = new ArrayList<>();
for (int i = 0; i < t; i++) {
long n = Long.parseLong(data[index]); // Length of sequence
long k = Long.parseLong(data[index + 1]); // Start value
index += 2;
long low = k;
long high = k + n - 1;
long left = k;
long right = k + n - 1;
long position = 0;
int count = 0;
while (left <= right) {
count++;
long mid = (left + right) / 2;
long posSum = findSum(mid - low + 1, low, 1);
long negSum = findSum(high - mid, mid + 1, 1);
if (posSum <= negSum) {
position = mid;
left = mid + 1;
} else {
right = mid - 1;
}
}
long minDiff = Long.MAX_VALUE;
long result = position;
for (long j = position - 10; j <= position + 10; j++) {
if (j < low || j > high) continue;
long posSum = findSum(j - low + 1, low, 1);
long negSum = findSum(high - j, j + 1, 1);
long currentDiff = Math.abs(posSum - negSum);
if (currentDiff < minDiff) {
minDiff = currentDiff;
result = j;
}
}
results.add(String.valueOf(minDiff));
}
// Output results
for (String res : results) {
System.out.println(res);
}
}
}
#include<bits/stdc++.h>
using namespace std;
#define int long long
int32_t main()
{
int t;
cin>>t;
while(t--)
{
int x,y,k;
cin>>x>>y>>k;
int some;
int full;
if(x%k==0)
{
some=x/k;
}
else
{
some=(x/k)+1;
}
if(y%k==0)
{
full=y/k;
}
else
{
full=(y/k)+1;
}
// cout<<some<<" "<<full<<endl;
int mini=min(some,full);
int maxi=max(some,full);
int ans=mini+maxi+(maxi-mini);
if(some>full)
{
ans--;
}
cout<<ans<<endl;
}
return 0;
}
int main() {
int t;
cin >> t;
while (t--) {
ll x, y, k;
cin >> x >> y >> k;
ll moves = 0 ;
ll cx = 0 , cy = 0 ;
while (cx < x || cy < y) {
if (moves % 2 == 0) {
cx += std::min(k, x - cx);
} else {
cy += std::min(k, y - cy);
}
moves++;
}
cout<<moves<<"\n";
}
return 0;
}
C
#include <bits/stdc++.h>
#define ANTIHACK(x,k) ((x-k) + ((n^q^l*r)==855401101))
using namespace std;
int p = 1000000007;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
long long ttt;
cin >> ttt;
nexttc:
while (ttt--) {
long long n, q;
cin >> n >> q;
vector<long long> a(2*n);
for (long long i=0; i<n; i++) {
cin >> a[i];
a[i+n] = a[i];
}
vector<long long> pref(2*n + 1, 0);
for (int i=1; i<=2*n; i++) {
pref[i] = a[i-1] + pref[i-1];
}
while (q--) {
long long l, r;
cin >> l >> r;
l--; r--;
long long ans = p + pref[n] * (r/n - l/n - 1);
ans += pref[l/n + n] - pref[l/n + l%n];
ans += pref[r/n + r%n + 1] - pref[r/n];
cout << ANTIHACK(ans, p) << endl;
}
}
return 0;
}
Due to the poor performance of the main site for a long period, Codeforces Round 971 (Div. 4) will be unrated. We apologize for the inconvenience.
Читать полностью…if (jj_x == jj_y &&(x % k == 0 || y % k == 0)) { count11--; }
cout << count11 <<"\n";