# Educational Codeforces Round 1

## A. Tricky Sum

time limit per test1 second memory limit per test256 megabytes

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.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.
Examplesinput

2
4
1000000000

output

-4
499999998352516354

Note

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

#### 签到题被自己蠢到了，竟然写了20分钟

#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define dep(i,a,b) for(int i=(a);i>=(b);--i)
#define pb push_back
typedef long long ll;
const int maxn=(int)2e5+100;
ll _pow,tot;
int main(){
tot=1;
for(ll i=0;_pow[i-1]<=1e9+100;++i){
_pow[i]=pow(2,i);
if(i>0) tot[i]=tot[i-1]+_pow[i];
}
int T;cin>>T;
while(T--){
ll n; cin>>n;
ll ans=0; ans=(1+n)*n/2;
int pos=0;
for(pos=0;;++pos) if(_pow[pos]>n) break;
ans-=tot[pos-1]*2;
printf("%lld\n",ans);
}
}


## B. Queries on a String

time limit per test2 seconds memory limit per test256 megabytes

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... riki 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.
Examplesinput

abacaba
2
3 6 1
1 4 2

output

baabcaa

Note

The sample is described in problem statement.

#### 直接模拟

#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define dep(i,a,b) for(int i=(a);i>=(b);--i)
#define pb push_back
typedef long long ll;
const int maxn=(int)2e5+100;
string s;
int main(){
cin>>s;
int q;cin>>q;
while(q--){
int l,r,k;scanf("%d%d%d",&l,&r,&k); k%=(r-l+1);
string tem1=s.substr(l-1,r-k-l+1),tem2=s.substr(r-k,k);
tem2+=tem1;
rep(i,0,r-l) s[i+l-1]=tem2[i];
}
cout<<s<<endl;
}


#### 也学了一手rotate函数

#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define dep(i,a,b) for(int i=(a);i>=(b);--i)
#define pb push_back
typedef long long ll;
const int maxn=(int)2e5+100;
string s;
int main(){
cin>>s;
int q;scanf("%d",&q);
while(q--){
int l,r,k; scanf("%d%d%d",&l,&r,&k); --l;
k%=(r-l);
rotate(s.begin()+l,s.begin()+r-k, s.begin()+r);
}
cout<<s<<"\n";
return 0;
}


## C. Nearest vectors

time limit per test2 seconds memory limit per test256 megabytes

You are given the set of vectors on the plane, each of them starting at the origin. Your task is to find a pair of vectors with the minimal non-oriented angle between them.

Non-oriented angle is non-negative value, minimal between clockwise and counterclockwise direction angles. Non-oriented angle is always between 0 and π. For example, opposite directions vectors have angle equals to π.Input

First line of the input contains a single integer n (2 ≤ n ≤ 100 000) — the number of vectors.

The i-th of the following n lines contains two integers xi and yi (|x|, |y| ≤ 10 000, x2 + y2 > 0) — the coordinates of the i-th vector. Vectors are numbered from 1 to n in order of appearing in the input. It is guaranteed that no two vectors in the input share the same direction (but they still can have opposite directions).Output

Print two integer numbers a and b (a ≠ b) — a pair of indices of vectors with the minimal non-oriented angle. You can print the numbers in any order. If there are many possible answers, print any.
Examplesinput

4
-1 0
0 -1
1 0
1 1

output

3 4

input

6
-1 0
0 -1
1 0
1 1
-4 -5
-4 -6

output

6 5

#### 极角排序，但是卡精度，不开long double会wa

#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define dep(i,a,b) for(int i=(a);i>=(b);--i)
#define pi acos(-1.0)
#define pb push_back
typedef long long ll;
const int maxn=(int)1e5+100;
struct node{
int x,y,id; long double k;
}p[maxn];
int cmp(node a,node b){return a.k<b.k;}
int main(){
int n;cin>>n;
rep(i,1,n){
scanf("%d%d",&p[i].x,&p[i].y); p[i].id=i;
p[i].k=atan2(p[i].y,p[i].x);
} sort(p+1,p+1+n,cmp);
long double ans=0;
if(p.k<0) ans+=p.k+2*pi-p[n].k;
else ans=p[n].k-p.k;
int pos1=p[n].id,pos2=p.id;
rep(i,2,n){
long double tem=p[i].k-p[i-1].k;
if(tem<ans){
ans=tem;
pos1=p[i].id;pos2=p[i-1].id;
}
}
printf("%d %d\n",pos1,pos2);
}


## D. Igor In the Museum

time limit per test1 second memory limit per test256 megabytes

Igor is in the museum and he wants to see as many pictures as possible.

Museum can be represented as a rectangular field of n × m cells. Each cell is either empty or impassable. Empty cells are marked with '.', impassable cells are marked with '*'. Every two adjacent cells of different types (one empty and one impassable) are divided by a wall containing one picture.

At the beginning Igor is in some empty cell. At every moment he can move to any empty cell that share a side with the current one.

For several starting positions you should calculate the maximum number of pictures that Igor can see. Igor is able to see the picture only if he is in the cell adjacent to the wall with this picture. Igor have a lot of time, so he will examine every picture he can see.Input

First line of the input contains three integers nm and k (3 ≤ n, m ≤ 1000, 1 ≤ k ≤ min(n·m, 100 000)) — the museum dimensions and the number of starting positions to process.

Each of the next n lines contains m symbols '.', '*' — the description of the museum. It is guaranteed that all border cells are impassable, so Igor can't go out from the museum.

Each of the last k lines contains two integers x and y (1 ≤ x ≤ n, 1 ≤ y ≤ m) — the row and the column of one of Igor's starting positions respectively. Rows are numbered from top to bottom, columns — from left to right. It is guaranteed that all starting positions are empty cells.Output

Print k integers — the maximum number of pictures, that Igor can see if he starts in corresponding position.
Examplesinput

5 6 3
******
*..*.*
******
*....*
******
2 2
2 5
4 3

output

6
4
10

input

4 4 1
****
*..*
*.**
****
3 2

output

8

#### BFS裸题，用并查集存了联通块

#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define dep(i,a,b) for(int i=(a);i>=(b);--i)
#define pi acos(-1.0)
#define pb push_back
typedef long long ll;
const int maxn=(int)1e5+100;
int n,m,k,sx,sy;
char mp;
int pre[(int)1e6+1010],ans[(int)1e6+1010],vis;
int dirx={1,-1,0,0},diry={0,0,1,-1};
struct pos{int x,y;};
int find(int x){return pre[x]==x?x:pre[x]=find(pre[x]);}
void join(int x,int y){
x=find(x);y=find(y);
if(x!=y) pre[x]=y;
}
int f(int x,int y){return (x-1)*m+y;}
int ck(int x,int y){
if(vis[x][y]||x<1||x>n||y<1||y>m) return 0;
if(mp[x][y]=='*') return -1;
return 1;
}
void bfs(int x,int y){
queue<pos> q; pos cur,nex;
cur.x=x;cur.y=y; int tot=0;
int fa=f(x,y);
q.push(cur); vis[x][y]=1;
while(!q.empty()){
cur=q.front(); q.pop();
rep(i,0,3){
nex.x=cur.x+dirx[i];
nex.y=cur.y+diry[i];
if(ck(nex.x,nex.y)==-1) tot++;
else if(ck(nex.x,nex.y)==1){
q.push(nex); vis[nex.x][nex.y]=1;
join(fa,f(nex.x,nex.y));
}
}
}
ans[find(f(x,y))]=tot;
}
int main(){
cin>>n>>m>>k;
rep(i,0,n*m) pre[i]=i;
rep(i,1,n) scanf("%s",mp[i]+1);
rep(i,1,n) rep(j,1,m) if(mp[i][j]=='.'&&!vis[i][j]) bfs(i,j);
while(k--){
int x,y;scanf("%d%d",&x,&y);
printf("%d\n",ans[find(f(x,y))]);
}
}


## E. Chocolate Bar

time limit per test2 seconds memory limit per test256 megabytes

You have a rectangular chocolate bar consisting of n × m single squares. You want to eat exactly k squares, so you may need to break the chocolate bar.

In one move you can break any single rectangular piece of chocolate in two rectangular pieces. You can break only by lines between squares: horizontally or vertically. The cost of breaking is equal to square of the break length.

For example, if you have a chocolate bar consisting of 2 × 3 unit squares then you can break it horizontally and get two 1 × 3 pieces (the cost of such breaking is 32 = 9), or you can break it vertically in two ways and get two pieces: 2 × 1 and 2 × 2 (the cost of such breaking is 22 = 4).

For several given values nm and k find the minimum total cost of breaking. You can eat exactly k squares of chocolate if after all operations of breaking there is a set of rectangular pieces of chocolate with the total size equal to k squares. The remaining n·m - ksquares are not necessarily form a single rectangular piece.Input

The first line of the input contains a single integer t (1 ≤ t ≤ 40910) — the number of values nm and k to process.

Each of the next t lines contains three integers nm and k (1 ≤ n, m ≤ 30, 1 ≤ k ≤ min(n·m, 50)) — the dimensions of the chocolate bar and the number of squares you want to eat respectively.Output

For each nm and k print the minimum total cost needed to break the chocolate bar, in order to make it possible to eat exactly k squares.
Examplesinput

4
2 2 1
2 2 3
2 2 2
2 2 4

output

5
5
4
0

Note

In the first query of the sample one needs to perform two breaks:

• to split 2 × 2 bar into two pieces of 2 × 1 (cost is 22 = 4),
• to split the resulting 2 × 1 into two 1 × 1 pieces (cost is 12 = 1).

In the second query of the sample one wants to eat 3 unit squares. One can use exactly the same strategy as in the first query of the sample.

#### 很典型一道DP，考虑到大小只有30*30*50，直接暴力

#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define dep(i,a,b) for(int i=(a);i>=(b);--i)
#define pi acos(-1.0)
#define pb push_back
typedef long long ll;
const int maxn=(int)1e5+100;
int dp;
void work(){
memset(dp,0x3f,sizeof(dp));
rep(i,0,30) rep(j,0,30){
dp[i][j]=0;
if(i*j<=50) dp[i][j][i*j]=0;
}
rep(i,1,30) rep(j,1,30) rep(k,1,min(i*j,50)){
rep(h,1,i/2) rep(kk,0,min(k,h*j))
dp[i][j][k]=min(dp[i][j][k],dp[h][j][kk]+dp[i-h][j][k-kk]+j*j);
rep(l,1,j/2) rep(kk,0,min(k,l*i))
dp[i][j][k]=min(dp[i][j][k],dp[i][l][kk]+dp[i][j-l][k-kk]+i*i);
}
}
int main(){
work();
int T;cin>>T;
while(T--){
int n,m,k;scanf("%d%d%d",&n,&m,&k);
printf("%d\n",dp[n][m][k]);
}
}


0 评论