A - Inna and Choose Options

Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Description

There always is something to choose from! And now, instead of "Noughts and Crosses", Inna choose a very unusual upgrade of this game. The rules of the game are given below:

There is one person playing the game. Before the beginning of the game he puts 12 cards in a row on the table. Each card contains a character: "X" or "O". Then the player chooses two positive integers a and b(a·b = 12), after that he makes a table of size a × b from the cards he put on the table as follows: the first b cards form the first row of the table, the second b cards form the second row of the table and so on, the last b cards form the last (number a) row of the table. The player wins if some column of the table contain characters "X" on all cards. Otherwise, the player loses.

Inna has already put 12 cards on the table in a row. But unfortunately, she doesn't know what numbers a and b to choose. Help her win the game: print to her all the possible ways of numbers a, b that she can choose and win.

Input

The first line of the input contains integer t(1 ≤ t ≤ 100). This value shows the number of sets of test data in the input. Next follows the description of each of the t tests on a separate line.

The description of each test is a string consisting of 12 characters, each character is either "X", or "O". The i-th character of the string shows the character that is written on the i-th card from the start.

Output

For each test, print the answer to the test on a single line. The first number in the line must represent the number of distinct ways to choose the pair a, b. Next, print on this line the pairs in the format axb. Print the pairs in the order of increasing first parameter (a). Separate the pairs in the line by whitespaces.

Sample Input

Input
4 OXXXOXOOXOOX OXOXOXOXOXOX XXXXXXXXXXXX OOOOOOOOOOOO
Output
3 1x12 2x6 4x3 4 1x12 2x6 3x4 6x2 6 1x12 2x6 3x4 4x3 6x2 12x1 0
题意:给出12张卡片,选择ab使得a*b=12,则把卡片依次分成a行,每行b个,如果某些列全是'X',则win,求得win的方案。
#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
using namespace std;
#define ll long long
#define MOD 100000
#define N 110
#define M 4 int ans;
int res[];
char s[];
int x[]={,,,,,}; int main()
{
int T;
int i,j,k;
scanf("%d",&T);
while(T--)
{
ans=;
scanf("%s",s);
for(i=;i<;i++)
{
int a=x[i];
int b=/x[i];
int flag=;
for(k=;k<b;k++)
{
for(j=;j<a;j++)
{
if(s[k+j*b]!='X') break;
}
if(j==a)
{
flag=;
break;
}
}
if(flag)
res[++ans]=x[i];
}
printf("%d",ans);
for(int i=;i<=ans;i++) printf(" %dx%d",res[i],/res[i]);
printf("\n");
}
return ;
}
B - Inna and New Matrix of Candies

Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Description

Inna likes sweets and a game called the "Candy Matrix". Today, she came up with the new game "Candy Matrix 2: Reload".

The field for the new game is a rectangle table of size n × m. Each line of the table contains one cell with a dwarf figurine, one cell with a candy, the other cells of the line are empty. The game lasts for several moves. During each move the player should choose all lines of the matrix where dwarf is not on the cell with candy and shout "Let's go!". After that, all the dwarves from the chosen lines start tosimultaneously move to the right. During each second, each dwarf goes to the adjacent cell that is located to the right of its current cell. The movement continues until one of the following events occurs:

  • some dwarf in one of the chosen lines is located in the rightmost cell of his row;
  • some dwarf in the chosen lines is located in the cell with the candy.

The point of the game is to transport all the dwarves to the candy cells.

Inna is fabulous, as she came up with such an interesting game. But what about you? Your task is to play this game optimally well. Specifically, you should say by the given game field what minimum number of moves the player needs to reach the goal of the game.

Input

The first line of the input contains two integers n and m(1 ≤ n ≤ 1000; 2 ≤ m ≤ 1000).

Next n lines each contain m characters — the game field for the "Candy Martix 2: Reload". Character "*" represents an empty cell of the field, character "G" represents a dwarf and character "S" represents a candy. The matrix doesn't contain other characters. It is guaranteed that each line contains exactly one character "G" and one character "S".

Output

In a single line print a single integer — either the minimum number of moves needed to achieve the aim of the game, or -1, if the aim cannot be achieved on the given game field.

Sample Input

Input
3 4 *G*S G**S *G*S
Output
2
Input
1 3 S*G
Output
-1

题意:有一个n*m的矩阵,其中*代表该格是空的,G代表该格有一个小矮人,S代表该格有一个糖果。每次都要选中所有有小矮人还没到达糖果格的那行,选中的这些行的小矮人都要向右走,如果有一行碰到这两种情况之一,所有行的小矮人都要停止向右运动,两种情况是:小矮人遇到糖果就停止运动,小矮人走到最右边了也停止运动。求最少执行多少次才能让所有的小矮人都能到达糖果格子,如果不能完成输出-1。

#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
using namespace std;
#define ll long long
#define INF 0x3fffffff
#define N 1010 int n,m;
int ans;
int flag;
int now[N];
bool vis[N];
char mpt[N][N]; int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
flag=;
for(int i=;i<=n;i++)
{
scanf("%s",mpt[i]+);
for(int j=;j<=m;j++)
{
if(mpt[i][j]=='G')
{
now[i]=j;
break;
}
else if(mpt[i][j]=='S')
{
flag=;
break;
}
}
}
if(!flag)
{
printf("-1");
continue;
}
ans=;
memset(vis,,sizeof(vis));
while()
{
int Min=INF;
for(int i=;i<=n;i++)
{
if(vis[i]) continue;
for(int j=now[i];j<=m;j++)
{
if(mpt[i][j]=='S' || j==m)
{
Min=min(Min,j-now[i]);
break;
}
}
}
if(Min==INF) break;
ans++;
for(int i=;i<=n;i++)
{
now[i]+=Min;
if(mpt[i][now[i]]=='S' || now[i]==m) vis[i]=;
}
}
printf("%d\n",ans);
}
return ;
}
C - Inna and Huge Candy Matrix

Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Description

Inna and Dima decided to surprise Sereja. They brought a really huge candy matrix, it's big even for Sereja! Let's number the rows of the giant matrix from 1 to n from top to bottom and the columns — from 1 to m, from left to right. We'll represent the cell on the intersection of the i-th row and j-th column as (i, j). Just as is expected, some cells of the giant candy matrix contain candies. Overall the matrix has p candies: the k-th candy is at cell (xk, yk).

The time moved closer to dinner and Inna was already going to eat p of her favourite sweets from the matrix, when suddenly Sereja (for the reason he didn't share with anyone) rotated the matrix x times clockwise by 90 degrees. Then he performed the horizontal rotate of the matrix y times. And then he rotated the matrix z times counterclockwise by 90 degrees. The figure below shows how the rotates of the matrix looks like.

Inna got really upset, but Duma suddenly understood two things: the candies didn't get damaged and he remembered which cells contained Inna's favourite sweets before Sereja's strange actions. Help guys to find the new coordinates in the candy matrix after the transformation Sereja made!

Input

The first line of the input contains fix integers nmxyzp(1 ≤ n, m ≤ 109; 0 ≤ x, y, z ≤ 109; 1 ≤ p ≤ 105).

Each of the following p lines contains two integers xkyk(1 ≤ xk ≤ n; 1 ≤ yk ≤ m) — the initial coordinates of the k-th candy. Two candies can lie on the same cell.

Output

For each of the p candies, print on a single line its space-separated new coordinates.

Sample Input

Input
3 3 3 1 1 9 1 1 1 2 1 3 2 1 2 2 2 3 3 1 3 2 3 3
Output
1 3 1 2 1 1 2 3 2 2 2 1 3 3 3 2 3 1

Hint

Just for clarity. Horizontal rotating is like a mirroring of the matrix. For matrix:

QWER      REWQ  ASDF  ->  FDSA ZXCV      VCXZ
题意:有三种变换,顺时针旋转90度,镜像,逆时针旋转90度,然后给出矩形大小、翻转次数以及开始坐标。
#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
using namespace std;
#define ll long long
#define INF 0x3fffffff
#define N 100010 int n,m,x,y,z,k;
pair<int,int> p[N];
int main()
{
while(scanf("%d%d%d%d%d%d",&n,&m,&x,&y,&z,&k)!=EOF)
{
for(int i=;i<=k;i++) scanf("%d%d",&p[i].first,&p[i].second);
x=x%;
y=y%;
z=z%; for(int i=;i<=x;i++) //x
{
for(int j=;j<=k;j++)
{
int tmp=p[j].first;
p[j].first=p[j].second;
p[j].second=n+-tmp;
}
swap(n,m);
}
for(int i=;i<=y;i++) //y
{
for(int j=;j<=k;j++)
{
p[j].second=m+-p[j].second;
}
}
for(int i=;i<=z;i++)
{
swap(n,m);
for(int j=;j<=k;j++)
{
int tmp=p[j].second;
p[j].second=p[j].first;
p[j].first=n+-tmp;
}
}
for(int i=;i<=k;i++) printf("%d %d\n",p[i].first,p[i].second);
}
return ;
}
D - Dima and Bacteria

Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Description

Dima took up the biology of bacteria, as a result of his experiments, he invented k types of bacteria. Overall, there are n bacteria at his laboratory right now, and the number of bacteria of type i equals ci. For convenience, we will assume that all the bacteria are numbered from 1 to n. The bacteria of type ci are numbered from  to .

With the help of special equipment Dima can move energy from some bacteria into some other one. Of course, the use of such equipment is not free. Dima knows m ways to move energy from some bacteria to another one. The way with number i can be described with integersuivi and xi mean that this way allows moving energy from bacteria with number ui to bacteria with number vi or vice versa for xidollars.

Dima's Chef (Inna) calls the type-distribution correct if there is a way (may be non-direct) to move energy from any bacteria of the particular type to any other bacteria of the same type (between any two bacteria of the same type) for zero cost.

As for correct type-distribution the cost of moving the energy depends only on the types of bacteria help Inna to determine is the type-distribution correct? If it is, print the matrix d with size k × k. Cell d[i][j] of this matrix must be equal to the minimal possible cost of energy-moving from bacteria with type i to bacteria with type j.

Input

The first line contains three integers n, m, k(1 ≤ n ≤ 105; 0 ≤ m ≤ 105; 1 ≤ k ≤ 500). The next line contains k integersc1, c2, ..., ck(1 ≤ ci ≤ n). Each of the next m lines contains three integers ui, vi, xi(1 ≤ ui, vi ≤ 105; 0 ≤ xi ≤ 104). It is guaranteed that .

Output

If Dima's type-distribution is correct, print string «Yes», and then k lines: in the i-th line print integers d[i][1], d[i][2], ..., d[i][k](d[i][i] = 0). If there is no way to move energy from bacteria i to bacteria j appropriate d[i][j] must equal to -1. If the type-distribution isn't correct print «No».

Sample Input

Input
4 4 2 1 3 2 3 0 3 4 0 2 4 1 2 1 2
Output
Yes 0 2 2 0
Input
3 1 2 2 1 1 2 0
Output
Yes 0 -1 -1 0
Input
3 2 2 2 1 1 2 0 2 3 1
Output
Yes 0 1 1 0
Input
3 0 2 1 2
Output
No
题意:给出n,m和k,表示有n个细菌,m种仪器和k种细菌,给出k种细菌的数量ci,然后每个细菌按照种类排成一排(所以有第i种细菌的序号从∑(1≤j≤i-1)cj + 1 到∑(1≤j≤i)cj);接下来给出m种仪器,有u,v,x三个值,表示说从可以在第u,v号细菌之间移动能量,代价为x。请帮助博士判断说这些细菌是否正确,正确的前提条件是说同种细菌之间移动能量为0,如果正确,给出两两细菌(种类)间移动能量的最小值。
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define INF 0x3f3f3f3f
#define N 100010
#define M 510 int n,m,k;
int f[N];
int num[N];
int belong[N];
int mpt[M][M]; int Find(int x)
{
if(x!=f[x]) f[x]=Find(f[x]);
return f[x];
}
void UN(int x,int y)
{
x=Find(x);
y=Find(y);
f[y]=x;
}
bool judge()
{
int i,j,p;
for(i=,p=;i<=k;i++)
{
int x=Find(p);
for(j=;j<=num[i];j++)
if(Find(p++)!=x) return ;
}
return ;
}
void floyd()
{
for(int p=;p<=k;p++)
{
for(int i=;i<=k;i++)
{
for(int j=;j<=k;j++)
{
mpt[i][j]=min(mpt[i][j],mpt[i][p]+mpt[p][j]);
}
}
}
}
int main()
{
int i,j,p;
while(scanf("%d%d%d",&n,&m,&k)!=EOF)
{
memset(mpt,INF,sizeof(mpt));
for(i=;i<=n;i++) f[i]=i;
for(i=,p=;i<=k;i++)
{
scanf("%d",&num[i]);
for(j=;j<=num[i];j++) belong[p++]=i;
}
int u,v,w,uu,vv;
while(m--)
{
scanf("%d%d%d",&u,&v,&w);
if(!w)
UN(u,v);
uu=belong[u];
vv=belong[v];
mpt[uu][vv]=mpt[vv][uu]=min(mpt[uu][vv],w);
}
if(!judge())
{
printf("No\n");
continue;
}
printf("Yes\n");
floyd();
for(i=;i<=k;i++)
{
for(j=;j<=k;j++)
{
if(i==j) mpt[i][j]=;
if(mpt[i][j]==INF) mpt[i][j]=-;
if(j<k) printf("%d ",mpt[i][j]);
else printf("%d",mpt[i][j]);
}
printf("\n");
}
}
return ;
}
E - Inna and Binary Logic

Time Limit:3000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Description

Inna is fed up with jokes about female logic. So she started using binary logic instead.

Inna has an array of n elements a1[1], a1[2], ..., a1[n]. Girl likes to train in her binary logic, so she does an exercise consisting of nstages: on the first stage Inna writes out all numbers from array a1, on the i-th (i ≥ 2) stage girl writes all elements of array ai, which consists of n - i + 1 integers; the k-th integer of array ai is defined as follows: ai[k] = ai - 1[kAND ai - 1[k + 1]. Here AND is bit-wise binary logical operation.

Dima decided to check Inna's skill. He asks Inna to change array, perform the exercise and say the sum of all  elements she wrote out during the current exercise.

Help Inna to answer the questions!

Input

The first line contains two integers n and m(1 ≤ n, m ≤ 105) — size of array a1 and number of Dima's questions. Next line contains nintegers a1[1], a1[2], ..., a1[n](0 ≤ ai ≤ 105) — initial array elements.

Each of next m lines contains two integers — Dima's question description. Each question consists of two integers pi, vi(1 ≤ pi ≤ n; 0 ≤ vi ≤ 105). For this question Inna should make a1[pi] equals vi, and then perform the exercise. Please, note that changes are saved from question to question.

Output

For each question print Inna's answer on a single line.

Sample Input

Input
3 4 1 1 1 1 1 2 2 3 2 1 2
Output
6 4 7 12
题意:给出n个数,m个操作,以及a1数组的每个数,然后往下a2数组个数比a1数组少一个数,a2[k]=a2[k]&a2[k+1],直到只有一个数,m次更改a1数组中某个数的值,求这些数组所有数的和。
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define ll long long
#define N 100010
#define M 18 ll a[N];
ll ans1,ans2,ans; int main()
{
int i,j,k;
int n,m,x,y;
scanf("%d%d",&n,&m);
for(i=;i<=n;i++) scanf("%I64d",&a[i]); //计算初始情况下的ans
for(k=;k<M;k++)
{
for(i=;i<=n;i++)
{
if((a[i]>>k)&)
{
ans1=ans2=;
for(j=i+;j<=n;j++,i=j)
{
if((a[j]>>k)&) ans2++;
else break;
}
ans+=(ans2*(ans2+)/)<<k;
}
}
}
//m次操作
for(i=;i<m;i++)
{
scanf("%d%d",&x,&y);
for(k=;k<M;k++)
{
ans1=ans2=;
for(j=x-;j>=;j--)
{
if((a[j]>>k)&) ans1++;
else break;
}
for(j=x+;j<=n;j++)
{
if((a[j]>>k)&) ans2++;
else break;
}
if((a[x]>>k)&) ans-=(ans1*ans2)<<k;
if((y>>k)&) ans+=(ans1*ans2)<<k;
}
a[x]=y;
printf("%I64d\n",ans);
}
return ;
}

CodeForces 400的更多相关文章

  1. codeforces 400 D Dima and Bacteria【并查集 Floyd】

    题意:给出n个点,分别属于k个集合,判断每个集合里面的点的距离都为0,为0的话输出yes,并输出任意两个集合之间的最短路 这道题目有两个地方不会处理, 先是n个点,分别属于k个集合,该怎么记录下来这里 ...

  2. codeforces 400 C Inna and Huge Candy Matrix【模拟】

    题意:给出一个矩形的三种操作,顺时针旋转,逆时针旋转,对称,给出原始坐标,再给出操作数,问最后得到的坐标 画一下模拟一下操作就可以找到规律了 #include<iostream> #inc ...

  3. ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) D. The Door Problem 2-SAT

    题目链接:http://codeforces.com/contest/776/problem/D D. The Door Problem time limit per test 2 seconds m ...

  4. ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) A map B贪心 C思路前缀

    A. A Serial Killer time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  5. ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) A

    Our beloved detective, Sherlock is currently trying to catch a serial killer who kills a person each ...

  6. 【第400篇题解纪念2016年10月28日】【28.10%】【codeforces 617E】XOR and Favorite Number

    time limit per test4 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  7. ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined)

    前四题比较水,E我看出是欧拉函数傻逼题,但我傻逼不会,百度了下开始学,最后在加时的时候A掉了 AC:ABCDE Rank:182 Rating:2193+34->2227 终于橙了,不知道能待几 ...

  8. Codeforces Round #400

    最近好像总是有点不想打,专题也刷不动,还是坚持这做了一场,虽然打到一半就没打了...(反正通常都只能做出两题) 感觉自己切水题越来越熟练了,然而难题还是不会做.. A题,水,用vector存下来就行了 ...

  9. 【2-SAT】【并查集】ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) D. The Door Problem

    再来回顾一下2-SAT,把每个点拆点为是和非两个点,如果a能一定推出非b,则a->非b,其他情况同理. 然后跑强连通分量分解,保证a和非a不在同一个分量里面. 这题由于你建完图发现都是双向边,所 ...

随机推荐

  1. springmvc(五)----异常处理

    总结

  2. 九度OJ 1104 整除问题

    题目地址:http://ac.jobdu.com/problem.php?pid=1104 题目描述: 给定n,a求最大的k,使n!可以被a^k整除但不能被a^(k+1)整除. 输入: 两个整数n(2 ...

  3. S.O.L.I.D

    S.O.L.I.D.是一组面对面向对象设计的最佳实践的设计原则.术语来自Robert C.Martin的著作Agile Principles, Patterns, and Practices in C ...

  4. cache在spring中使用

    一:参考文章 (1)http://haohaoxuexi.iteye.com/blog/2123030  Spring使用Cache,这篇文章讲的比较详细. 注:本文是对参考文章和实际使用中经验的总结 ...

  5. CentOS7 yum安装配置

    一.安装必要包 yum install gcc 二.linux下安装 #下载 wget http://download.redis.io/releases/redis-3.0.0.tar.gz tar ...

  6. linux下python导出sybase 数据库 表记录的方式

    导出sybase 数据库 表记录的方式 1 执行启动sybase 数据库命令 code : dbeng7 gkdb 2 执行 连接sybase 数据库命令code : dbisql -c " ...

  7. AngularJS(3)-过滤器

    过滤器可以通过一个管道字符(|)和一个过滤器添加到表达式中.. 1.uppercase/lowercase 大小写过滤器 2.currency过滤器 3.向指令添加过滤器 过滤器可以通过一个管道字符( ...

  8. object-c 1

    多个参数的写法 (方法的数据类型)函数名:(参数1数据类型)参数1的数值的名字 参数2的名字: (参数2数据类型) 参数2值的名字 …. ; 举个例子,一个方法的定义: -(void) setKids ...

  9. 简单的powershell 批量生成身份证复印件

    用Powshell 脚本批量完成,代码比较简单,出生日期,图片我都没有改,图片用于测试,附件. cls; $path = Split-Path -Parent $MyInvocation.MyComm ...

  10. js日期范围初始化,得到前一个月的日期

    今天做时间范围的初始化设定,开始时间是当前时间的前一个月,终于找到完美的解决方案了. Date.prototype.format = function(format) {     var o = { ...