【CODEFORCES】 Educational Codeforces Round 1

本文解析了两个编程挑战赛题目:TrickySum要求计算特定规则下的整数和;QueriesonaString涉及字符串子串的循环移位操作。通过示例代码展示了问题解决的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这次比赛写的好蠢,只A了2前两道,第一题一开始还想错了……程序这东西还是要多练,最近也要比赛了,自己要加油~~

A. Tricky Sum
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

In this problem you are to calculate the sum of all integers from 1 to n, but you should take all powers of two with minus in the sum.

For example, for n = 4 the sum is equal to  - 1 - 2 + 3 - 4 =  - 4, because 12 and 4 are 2021 and 22 respectively.

Calculate the answer for t values of n.

Input

The first line of the input contains a single integer t (1 ≤ t ≤ 100) — the number of values of n to be processed.

Each of next t lines contains a single integer n (1 ≤ n ≤ 109).

Output

Print the requested sum for each of t integers n given in the input.

Sample test(s)
input
2
4
1000000000
output
-4
499999998352516354
Note

The answer for the first sample is explained in the statement.

题解:这一题其实就直接判有多少个2的次方就行了,可以先直接从1加到n,然后把所有2的次方减掉2便就OK。(一开始用了log算有多少个2的次方,结果因为误差WA了N次...)

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>

using namespace std;

long long sum1,sum2,t,n;
double g;

int main()
{
    scanf("%I64d",&t);
    while (t--)
    {
        scanf("%I64d",&n);
        sum1=((1+n)*n)/2;
        g=(log(n)/log(2));
        sum2=1;
        while (sum2<=n)
        {
            sum1=sum1-sum2*2;
            sum2=2*sum2;
        }
        if (t) printf("%I64d\n",sum1);
        else printf("%I64d",sum1);
    }
    return 0;
}


B. Queries on a String
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given a string s and should process m queries. Each query is described by two 1-based indices liri and integer ki. It means that you should cyclically shift the substring s[li... ri] ki times. The queries should be processed one after another in the order they are given.

One operation of a cyclic shift (rotation) is equivalent to moving the last character to the position of the first character and shifting all other characters one position to the right.

For example, if the string s is abacaba and the query is l1 = 3, r1 = 6, k1 = 1 then the answer is abbacaa. If after that we would process the query l2 = 1, r2 = 4, k2 = 2 then we would get the string baabcaa.

Input

The first line of the input contains the string s (1 ≤ |s| ≤ 10 000) in its initial state, where |s| stands for the length of s. It contains only lowercase English letters.

Second line contains a single integer m (1 ≤ m ≤ 300) — the number of queries.

The i-th of the next m lines contains three integers liri and ki (1 ≤ li ≤ ri ≤ |s|, 1 ≤ ki ≤ 1 000 000) — the description of the i-th query.

Output

Print the resulting string s after processing all m queries.

Sample test(s)
input
abacaba
2
3 6 1
1 4 2
output
baabcaa
Note

The sample is described in problem statement.


题解:这一题直接用函数连着滚就行了,直接模拟,没什么大技巧。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>

using namespace std;

char s[10005];
int d1[10005],d2[10005],f,l,r,k,m,n;

void solve(int d1[],int d2[],int l,int r,int k,int n)
{
    for (int i=0;i<l-1;i++) d2[i]=d1[i];
    for (int i=l-1,j=r-k;i<l+k-1;i++,j++) d2[i]=d1[j];
    for (int i=l+k-1,j=l-1;i<=r-1;i++,j++) d2[i]=d1[j];
    for (int i=r;i<n;i++)  d2[i]=d1[i];
}

int main()
{
    scanf("%s",s);
    n=strlen(s);
    for (int i=0;i<n;i++)
    {
        d1[i]=s[i]-55; d2[i]=d1[i];
    }
    scanf("%d",&m);
    f=1;
    while (m--)
    {
        scanf("%d%d%d",&l,&r,&k);
        k=k%(r-l+1);
        if (f) solve(d1,d2,l,r,k,n);
        else solve(d2,d1,l,r,k,n);
        f=!f;
    }
    if (f) for (int i=0;i<n;i++) printf("%c",d1[i]+55);
    else for (int i=0;i<n;i++) printf("%c",d2[i]+55);
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值