Codeforces Round #305 (Div. 2)

A. Mike and Fax
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

While Mike was walking in the subway, all the stuff in his back-bag dropped on the ground. There were several fax messages among them. He concatenated these strings in some order and now he has strings.

He is not sure if this is his own back-bag or someone else's. He remembered that there were exactlyk messages in his own bag, each was a palindrome string and all those strings had the same length.

He asked you to help him and tell him if he has worn his own back-bag. Check if the given strings is a concatenation of k palindromes of the same length.

Input

The first line of input contains string s containing lowercase English letters (1 ≤ |s| ≤ 1000).

The second line contains integer k (1 ≤ k ≤ 1000).

Output

Print "YES"(without quotes) if he has worn his own back-bag or "NO"(without quotes) otherwise.

Sample test(s)
Input
saba
2
Output
NO
Input
saddastavvat
2
Output
YES
Note

Palindrome is a string reading the same forward and backward.

In the second sample, the faxes in his back-bag can be "saddas" and "tavvat".


题意:给你一个字符串,问把它均分成k个子串,这些子串是否都是回文串。

题目链接:http://codeforces.com/contest/548/problem/A

代码清单:

#include<iostream>
#include<algorithm>
using namespace std;

int k;
string str;

void input(){
    cin>>str;
    cin>>k;
}

void solve(){
    bool ok=true;
    int length=str.size();
    if(length%k) cout<<"NO"<<endl;
    else{
        int n=length/k;
        for(int i=0;i<length;i+=n){
            for(int j=0;j<n/2;j++){
                if(str[i+j]!=str[i+n-j-1]){
                    ok=false;
                    break;
                }
            }if(!ok) break;
        }
        if(ok) cout<<"YES"<<endl;
        else cout<<"NO"<<endl;
    }
}

int main(){
    input();
    solve();
    return 0;
}

B. Mike and Fun
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Mike and some bears are playing a game just for fun. Mike is the judge. All bears except Mike are standing in ann × m grid, there's exactly one bear in each cell. We denote the bear standing in column numberj of row number i by(i, j). Mike's hands are on his ears (since he's the judge) and each bear standing in the grid has hands either on his mouth or his eyes.

They play for q rounds. In each round, Mike chooses a bear(i, j) and tells him to change his state i. e. if his hands are on his mouth, then he'll put his hands on his eyes or he'll put his hands on his mouth otherwise. After that, Mike wants to know the score of the bears.

Score of the bears is the maximum over all rows of number of consecutive bears with hands on their eyes in that row.

Since bears are lazy, Mike asked you for help. For each round, tell him the score of these bears after changing the state of a bear selected in that round.

Input

The first line of input contains three integers n,m and q (1 ≤ n, m ≤ 500 and1 ≤ q ≤ 5000).

The next n lines contain the grid description. There arem integers separated by spaces in each line. Each of these numbers is either0 (for mouth) or 1 (for eyes).

The next q lines contain the information about the rounds. Each of them contains two integersi and j (1 ≤ i ≤ n and1 ≤ j ≤ m), the row number and the column number of the bear changing his state.

Output

After each round, print the current score of the bears.

Sample test(s)
Input
5 4 5
0 1 1 0
1 0 0 1
0 1 1 0
1 0 0 1
0 0 0 0
1 1
1 4
1 1
4 2
4 3
Output
3
4
3
3
4

题意:给你一个n*m的(0,1)组成的矩阵,然后给你p个操作,每次操作给出一个点q(x,y),如果点q值是0,则变成1;如果q值是1,则变成0。然后求出每次操作的最大的连续的1的个数。

题目链接:http://codeforces.com/contest/548/problem/B

代码清单:

#include<iostream>
#include<algorithm>
using namespace std;
const int maxn = 500 + 5;
int n,m,q,x,y;
int vis[maxn][maxn];

void input(){
    cin>>n>>m>>q;
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            cin>>vis[i][j];
        }
    }
}

int find_max(){
    int max_num=0;
    for(int i=0;i<n;i++){
        int sum=0;
        for(int j=0;j<m;j++){
            if(vis[i][j]==1) sum++;
            else sum=0;
            max_num=max(max_num,sum);
        }
    }
    return max_num;
}

void solve(){
    if(vis[x-1][y-1]==0){
        vis[x-1][y-1]=1;
    }
    else{
        vis[x-1][y-1]=0;
    }
    cout<<find_max()<<endl;
}


int main(){
    input();
    while(q--){
        cin>>x>>y;
        solve();
    }return 0;
}

C. Mike and Frog
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Mike has a frog and a flower. His frog is named Xaniar and his flower is named Abol. Initially(at time0), height of Xaniar is h1 and height of Abol ish2. Each second, Mike waters Abol and Xaniar.

So, if height of Xaniar is h1 and height of Abol ish2, after one second height of Xaniar will become and height of Abol will become wherex1, y1, x2 andy2 are some integer numbers and denotes the remainder ofa modulo b.

Mike is a competitive programmer fan. He wants to know the minimum time it takes until height of Xania isa1 and height of Abol isa2.

Mike has asked you for your help. Calculate the minimum time or say it will never happen.

Input

The first line of input contains integer m (2 ≤ m ≤ 106).

The second line of input contains integers h1 anda1 (0 ≤ h1, a1 < m).

The third line of input contains integers x1 andy1 (0 ≤ x1, y1 < m).

The fourth line of input contains integers h2 anda2 (0 ≤ h2, a2 < m).

The fifth line of input contains integers x2 andy2 (0 ≤ x2, y2 < m).

It is guaranteed that h1 ≠ a1 andh2 ≠ a2.

Output

Print the minimum number of seconds until Xaniar reaches height a1 and Abol reaches height a2 or print -1 otherwise.

Sample test(s)
Input
5
4 2
1 1
0 1
2 3
Output
3
Input
1023
1 2
1 0
1 2
1 1
Output
-1

题意:每过一秒钟,h1=(x1*h1+y1)%m,h2=(x*h2+y2)%m。现在给你m,h1,a1,x1,y1,h2,x2,y2,a2,问你何时h1=a1,并且同时h2=a2。

分析:这道题就是让人各种心碎。猝不及防的小bug让你心力交瘁。我就不分析了,直接看代码注释吧。心塞……

代码清单:

#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn = 3050;
const int mod = 3031;
const int max_time = 10000000 + 5;

int m,h1,a1,x1,y1,h2,a2,x2,y2;
bool vis[maxn][maxn];

void input(){
    scanf("%d",&m);
    scanf("%d%d%d%d",&h1,&a1,&x1,&y1);
    scanf("%d%d%d%d",&h2,&a2,&x2,&y2);
}

bool get_zhouqi(int h,int a,int x,int y,ll &first,ll &zq){ //求解周期
    zq=0;
    int step=0;
    while(zq<=2*m){
        if(h==a){
            if(step==0){
                first=zq;  //第一次到达目标时间
                step++;
            }
            else{
                zq=zq-first; //得到周期,返回true
                return true;
            }
        }
        zq++;
        h=(int)(((ll)x*(ll)h+(ll)y)%(ll)m);
    }return false;
}

ll gcd(ll a,ll b){
    if(b==0) return a;
    return gcd(b,a%b);
}

void solve(){

    ll sum1,sum2,first1,zq1,first2,zq2;
    bool ok1=get_zhouqi(h1,a1,x1,y1,first1,zq1);
    bool ok2=get_zhouqi(h2,a2,x2,y2,first2,zq2);

    if(first1==first2){  //如果第一次到达目标的时间相同
        printf("%I64d\n",first1);
        return ;
    }
    if(!ok1&&!ok2){     //第一次到达目标时间不同,且没有周期
        printf("-1\n");
        return ;
    }
    if(!ok1){
        if(first1==first2+zq2){ //当frog没周期,但是第一次能到达a1,且时间等于Xaniar第二次到达a2
            printf("%I64d\n",first1);
        }
        else printf("-1\n");
        return ;
    }
    if(!ok2){
        if(first1+zq1==first2){ //当Xaniar没有周期,但是第一次能到达a2,且时间等于frog第二次到达a1
            printf("%I64d\n",first2);
        }
        else printf("-1\n");
        return ;
    }

    sum1=first1;
    sum2=first2;

    //first1+x*zq1 = first2+y*zq2 -> x*zq1-y*zq2=first2-first1 二元一次方程有整数解的判定
    //ax+by=c 有整数解的判定为:c能够整除gcd(a,b).

    if(abs(sum1-sum2)%gcd(zq1,zq2)){
        printf("-1\n");
        return ;
    }

    while(sum1!=sum2){
        if(sum1<sum2) sum1+=zq1;
        else sum2+=zq2;
    }
    printf("%I64d\n",sum1);
    return ;

}

int main(){
    input();
    solve();
    return 0;
}


 
    

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值