您的位置:首页 > 其它

codeforces 15 C&D&E

2018-02-24 12:19 260 查看
C. Industrial Nim

time limit per test
2 seconds

memory limit per test
64 megabytes

input
standard input

output
standard output

There are n stone quarries in Petrograd.
Each quarry owns mi dumpers
(1 ≤ i ≤ n). It is known that the first dumper of the i-th
quarry has xi stones
in it, the second dumper has xi + 1 stones
in it, the third has xi + 2,
and the mi-th
dumper (the last for the i-th quarry) has xi + mi - 1 stones
in it.
Two oligarchs play a well-known game Nim. Players take turns removing stones from dumpers. On each turn, a player can select any dumper and remove any non-zero amount of stones from
it. The player who cannot take a stone loses.
Your task is to find out which oligarch will win, provided that both of them play optimally. The oligarchs asked you not to reveal their names. So, let's call the one who takes the
first stone «tolik» and the other one «bolik».

Input
The first line of the input contains one integer number n (1 ≤ n ≤ 105)
— the amount of quarries. Then there follow n lines, each of them contains two space-separated integers xi and mi (1 ≤ xi, mi ≤ 1016)
— the amount of stones in the first dumper of the i-th quarry and the number of dumpers at the i-th
quarry.

Output
Output «tolik» if the oligarch who takes a stone first wins, and «bolik»
otherwise.

Examples

input

Copy

2
2 1
3 2


output
tolik


input

Copy

4
1 1
1 1
1 1
1 1


output
bolik


#include<bits/stdc++.h>
using namespace std;
#define ll long long
ll a[100],b[100];
const int t = 0x7fffffff;
ll rem1, rem2, xor1, xor2, ans, xi, mi;
int main(){
int testn;
ans = 0;
cin>>testn;
for (int i=0; i<testn; i++){
cin>>xi>>mi;
rem1 = (xi-1) % 4;
rem2 = (xi+mi-1) %4;
switch(rem1){
case 0 : xor1 = xi - 1;break;
case 1 : xor1 = 1;break;
case 2 : xor1 = xi;break;
case 3 : xor1 = 0;break;
}
switch(rem2){
case 0 : xor2 = xi+mi-1;break;
case 1 : xor2 = 1;break;
case 2 : xor2 = xi+mi;break;
case 3 : xor2 = 0;break;
}
if (xi == 1) ans ^= xor2;
else ans ^= xor1 ^ xor2;
}

if (ans == 0){
cout<<"bolik"<<endl;
}
else cout<<"tolik"<<endl;

}


D. Map

time limit per test
2 seconds

memory limit per test
128 megabytes

input
standard input

output
standard output

There is an area map that is a rectangular matrix n × m, each cell of the matrix contains the average height
of a corresponding area part. Peter works for a company that has to build several cities within this area, each of the cities will occupy a rectangle a × b cells
on the map. To start construction works in a particular place Peter needs to remove excess ground from the construction site where a new city will be built. To do so he chooses a cell of the minimum height within this site, and removes excess ground from other
cells of the site down to this minimum level. Let's consider that to lower the ground level from h2 to h1 (h1 ≤ h2)
they need to remove h2 - h1 ground
units.
Let's call a site's position optimal, if the amount of the ground removed from this site is minimal compared to other possible positions. Peter constructs cities according to the
following algorithm: from all the optimum site's positions he chooses the uppermost one. If this position is not unique, he chooses the leftmost one. Then he builds a city on this site. Peter repeats this process untill he can build at least one more city.
For sure, he cannot carry out construction works on the occupied cells. Would you, please, help Peter place cities according to the algorithm?

Input
The first line contains four space-separated integers: map sizes n, m and
city sizes a, b (1 ≤ a ≤ n ≤ 1000, 1 ≤ b ≤ m ≤ 1000).
Then there follow n lines, each contains m non-negative
space-separated numbers, describing the height matrix. Each number doesn't exceed 109.

Output
In the first line output k —
the amount of constructed cities. In each of the following k lines output 3 space-separated numbers — the
row number and the column number of the upper-left corner of a subsequent construction site, and the amount of the ground to remove from it. Output the sites in the order of their building up.

Examples

input

Copy

2 2 1 2
1 2
3 5


output
2
1 1 1
2 1 2


input

Copy

4 4 2 2
1 5 3 4
2 7 6 1
1 1 2 2
2 2 1 2


output
3
3 1 2
3 3 3
1 2 9


#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (ll i=a; i<n; i++)
#define pb push_back
#define pii pair<ll, ll>
#define fi first
#define se second
#define maxn 1005
#define maxint 0x7fffffff
#define ll long long
int que[maxn];
ll n, m, a, id, b, zuidigaodu, S, E;
int Q[maxn*maxn];
ll min_c;
int used[maxn*maxn];
int mp[maxn][maxn];
ll sum[maxn*2][maxn*2];
int minr[maxn][maxn];
ll get_id (ll x, ll y){
return (x*(m-b+1) + y) ;
}
struct square {
int x, y,original_id;
ll cost;
const bool operator < (const square &cp){
return (cost < cp.cost
|| ((cost == cp.cost) && (x<cp.x))
|| ((cost == cp.cost) && (x==cp.x) && (y<cp.y)));
}
}sqr[maxn*maxn];

int main(){
cin>>n>>m>>a>>b;
rep(i, 0, n){
rep(j, 0, m){
scanf("%d", &mp[i][j]);
}
}
///倒着不用处理边界
for (int i=n-1; i>=0; i--){
for (int j=m-1; j>=0; j--){
sum[i][j] = mp[i][j] + sum[i+1][j] + sum[i][j+1] - sum[i+1][j+1];
}
}
///正着才能处理左上角 不至于改变后面的状态
/*

*/
rep(i, 0, n-a+1){
rep(j, 0, m-b+1){
sum[i][j] = sum[i][j] - sum[i+a][j] - sum[i][j+b] + sum[i+a][j+b];
}
}
/*	rep(i, 0, n){
rep(j, 0, m){
cout<<sum[i][j]<<" ";
}
cout<<endl;
}
*/
///倒着处理方便表示以j为左端点右边的a个点的min
rep(i, 0, n){
S = E = 0;
/*
下标可有多种写法 但是必须使S指向第一个元素
while (S<=E && mp[i][que[E]]>mp[i][j]) 这种就是错的 必须满足两个条件才能到-1 导致初始化失败
否则S永远指向0 且 que[S] = 0
*/
for (int j=m-1; j>=0; j--){
while (S<E && mp[i][que[E-1]]>mp[i][j])
E--;
que[E++] = j;
while (S<E && que[S]>=j+b)
S++;
minr[i][j] = mp[i][que[S]];
}
}
///一样倒着 用mp储存二位结果节省空间 因为mp没用了

rep(j, 0, m){
S = E = 0;
for (int i=n-1; i>=0; i--){
while (S<E && minr[que[E-1]][j]>minr[i][j])
E--;
que[E++] = i;
while (S<E && que[S]>=i+a)
S++;
mp[i][j] = minr[que[S]][j];
}
}

memset(used, 0, sizeof (used));
rep(i, 0, n-a+1){
rep(j, 0, m-b+1){
id = get_id(i, j);
sqr[id].original_id = id;
sqr[id].x = i; sqr[id].y = j;
sqr[id].cost = sum[i][j] - (ll) a*b*mp[i][j];//herelong long保护
//cout<<"id = "<<id<<endl;
}
}
/*
rep(i, 0, (n-a+1) * (m-b+1)){
cout<<"original_id = "<<sqr[i].original_id<<endl;
cout<<"cost = "<<sqr[i].cost<<endl;
cout<<"x = "<<sqr[i].x<<" y = "
d4ae
<< sqr[i].y <<endl;
puts("");
}

puts("after\n");
rep(i, 0, (n-a+1) * (m-b+1)){
cout<<"original_id = "<<sqr[i].original_id<<endl;
cout<<"cost = "<<sqr[i].cost<<endl;
cout<<"x = "<<sqr[i].x<<" y = "<< sqr[i].y <<endl;
puts("");
}
*/
sort(sqr, sqr+(n-a+1)*(m-b+1));
ll cnt = 0;

rep(i, 0, (n-a+1)*(m-b+1)){
if (!used[sqr[i].original_id]){
Q[cnt++] = i;
rep(k, sqr[i].x-a+1, sqr[i].x+a){
rep(l, sqr[i].y-b+1, sqr[i].y+b){
if (k<0 || k>=(n-a+1) || l<0 || (l>=m-b+1)) continue;
used[get_id(k, l)] = 1;
}
}
}
}
printf("%lld\n", cnt);
rep(j, 0, cnt){
ll i = Q[j];
printf("%d %d %lld\n", sqr[i].x+1, sqr[i].y+1, sqr[i].cost);
}

//输出要加1
}


E. Triangles

time limit per test
1 second

memory limit per test
64 megabytes

input
standard input

output
standard output

Last summer Peter was at his granny's in the country, when a wolf attacked sheep in the nearby forest. Now he fears to walk through the forest, to walk round the forest, even to get out of the house. He explains this not by the fear of the wolf, but by a strange,
in his opinion, pattern of the forest that has n levels, where n is
an even number.
In the local council you were given an area map, where the granny's house is marked by point H,
parts of dense forest are marked grey (see the picture to understand better).
After a long time at home Peter decided to yield to his granny's persuasions and step out for a breath of fresh air. Being prudent, Peter plans the route beforehand. The route, that
Peter considers the most suitable, has the following characteristics:

it starts and ends in the same place — the granny's house;

the route goes along the forest paths only (these are the segments marked black in the picture);

the route has positive length (to step out for a breath of fresh air Peter has to cover some distance anyway);

the route cannot cross itself;

there shouldn't be any part of dense forest within the part marked out by this route;
You should find the amount of such suitable oriented routes modulo 1000000009.



The example of the area map for n = 12 is
given in the picture. Since the map has a regular structure, you can construct it for other n by analogy
using the example.

Input
The input data contain the only even integer n (2 ≤ n ≤ 106).

Output
Output the only number — the amount of Peter's routes modulo 1000000009.

Examples

input

Copy

2


output
10


input

Copy

4


output
74


#include<bits/stdc++.h>
#define ll long long int
#define mod 1000000009
using namespace std;

ll a=2,b=2,c=4,n;

int main(int argc, char const *argv[])
{
scanf("%I64d",&n);
a=2,b=2,c=4;
while(n-=2)
{
printf("n = %lld\n", n);
a=a*2%mod,c=c*(a-3)%mod,b=(b+c)%mod;
printf("a = %lld c = %lld b = %lld\n", a, c, b);
}
printf("%I64d\n",(b*b+1)*2%mod);
printf("%I64d\n",(b*b+1)*2- c*c*2 - c*2);
return 0;
}








每层从上一层走到巷子口都是4种  然后过巷子就是2^i-3 然后再乘上上面的走法

a是算单个巷子的指数

b是把每层结果加起来

c是这层以上所有巷子的乘法法则

或者这样说 每次都是沿着最左边走 只在2n层分叉

篮圈到绿圈4种 绿圈算作巷子入口 绿圈走到红圈13种 乘起来4*13



再乘上上面所有巷子
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: