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 1, 2 and 4 are 20, 21 and 22 respectively.
Calculate the answer for t values of n.
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).
Print the requested sum for each of t integers n given in the input.
2 4 1000000000
-4 499999998352516354
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;
}
You are given a string s and should process m queries. Each query is described by two 1-based indices li, ri 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.
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 li, ri and ki (1 ≤ li ≤ ri ≤ |s|, 1 ≤ ki ≤ 1 000 000) — the description of the i-th query.
Print the resulting string s after processing all m queries.
abacaba 2 3 6 1 1 4 2
baabcaa
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;
}