CodeForces 400

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[20];
char s[20];
int x[20]={1,2,3,4,6,12};

int main()
{
    int T;
    int i,j,k;
    scanf("%d",&T);
    while(T--)
    {
        ans=0;
        scanf("%s",s);
        for(i=0;i<6;i++)
        {
            int a=x[i];
            int b=12/x[i];
            int flag=0;
            for(k=0;k<b;k++)
            {
                for(j=0;j<a;j++)
                {
                    if(s[k+j*b]!='X') break;
                }
                if(j==a)
                {
                    flag=1;
                    break;
                }
            }
            if(flag)
                res[++ans]=x[i];
        }
        printf("%d",ans);
        for(int i=1;i<=ans;i++) printf(" %dx%d",res[i],12/res[i]);
        printf("\n");
    }
    return 0;
}

 

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=1;
        for(int i=1;i<=n;i++)
        {
            scanf("%s",mpt[i]+1);
            for(int j=1;j<=m;j++)
            {
                if(mpt[i][j]=='G')
                {
                    now[i]=j;
                    break;
                }
                else if(mpt[i][j]=='S')
                {
                    flag=0;
                    break;
                }
            }
        }
        if(!flag)
        {
            printf("-1");
            continue;
        }
        ans=0;
        memset(vis,0,sizeof(vis));
        while(1)
        {
            int Min=INF;
            for(int i=1;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=1;i<=n;i++)
            {
                now[i]+=Min;
                if(mpt[i][now[i]]=='S' || now[i]==m) vis[i]=1;
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}

 

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 n, m, x, y, z, p(1 ≤ n, m ≤ 109; 0 ≤ x, y, z ≤ 109; 1 ≤ p ≤ 105).

Each of the following p lines contains two integers xk, yk(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=1;i<=k;i++) scanf("%d%d",&p[i].first,&p[i].second);
        x=x%4;
        y=y%2;
        z=z%4;

        for(int i=1;i<=x;i++)   //x
        {
            for(int j=1;j<=k;j++)
            {
                int tmp=p[j].first;
                p[j].first=p[j].second;
                p[j].second=n+1-tmp;
            }
            swap(n,m);
        }
        for(int i=1;i<=y;i++)  //y
        {
            for(int j=1;j<=k;j++)
            {
                p[j].second=m+1-p[j].second;
            }
        }
        for(int i=1;i<=z;i++)
        {
            swap(n,m);
            for(int j=1;j<=k;j++)
            {
                int tmp=p[j].second;
                p[j].second=p[j].first;
                p[j].first=n+1-tmp;
            }
        }
        for(int i=1;i<=k;i++) printf("%d %d\n",p[i].first,p[i].second);
    }
    return 0;
}

 

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 integersui, vi 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=1,p=1;i<=k;i++)
    {
        int x=Find(p);
        for(j=1;j<=num[i];j++)
            if(Find(p++)!=x) return 0;
    }
    return 1;
}
void floyd()
{
    for(int p=1;p<=k;p++)
    {
        for(int i=1;i<=k;i++)
        {
            for(int j=1;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=0;i<=n;i++) f[i]=i;
        for(i=1,p=1;i<=k;i++)
        {
            scanf("%d",&num[i]);
            for(j=1;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=1;i<=k;i++)
        {
            for(j=1;j<=k;j++)
            {
                if(i==j) mpt[i][j]=0;
                if(mpt[i][j]==INF) mpt[i][j]=-1;
                if(j<k) printf("%d ",mpt[i][j]);
                else printf("%d",mpt[i][j]);
            }
            printf("\n");
        }
    }
    return 0;
}

 

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=1;i<=n;i++) scanf("%I64d",&a[i]);

    //计算初始情况下的ans
    for(k=0;k<M;k++)
    {
        for(i=1;i<=n;i++)
        {
            if((a[i]>>k)&1) 
            {
                ans1=ans2=1;
                for(j=i+1;j<=n;j++,i=j)
                {
                    if((a[j]>>k)&1) ans2++;
                    else break;
                }
                ans+=(ans2*(ans2+1)/2)<<k;
            }
        }
    }
    //m次操作
    for(i=0;i<m;i++)
    {
        scanf("%d%d",&x,&y);
        for(k=0;k<M;k++)
        {
            ans1=ans2=1;
            for(j=x-1;j>=0;j--)
            {
                if((a[j]>>k)&1)  ans1++;
                else break;
            }
            for(j=x+1;j<=n;j++)
            {
                if((a[j]>>k)&1) ans2++;
                else break;
            }
            if((a[x]>>k)&1) ans-=(ans1*ans2)<<k;
            if((y>>k)&1) ans+=(ans1*ans2)<<k;
        }
        a[x]=y;
        printf("%I64d\n",ans);
    }
    return 0;
}

 

posted @ 2015-03-17 14:27  哈特13  阅读(239)  评论(0编辑  收藏  举报