Codeforces Round #305 (Div. 2)

来源:互联网 发布:80端口被系统占用 编辑:程序博客网 时间:2024/06/06 19:52

A:http://codeforces.com/contest/548/problem/A
题意:给定字符串s和数字k。问s是否由多个长度为k的回文字符串构成。
思路:先判断是否字符串长度是否被k整除,若是,直接模拟判断是否由多个长度为k的回文字符串构成。

B:http://codeforces.com/contest/548/problem/B
题意:给定一个n*m的矩阵,只包含0和1。q次操作。每次操作读入一个i和j,将(i,j)位置的数字取反。求整个矩阵中每行连续1的最大个数。
思路:q次操作前,先求出每行连续1的最大个数存在数组中,每次操作处理第i行,然后求最大值。

C:http://codeforces.com/contest/548/problem/C
题意:输入m,h1,a1,x1,y1,h2,a2,x2,y2。每单位时间做的操作如下

    h1 = ((h1 * x1) + y1) % m    h2 = ((h2 * x2) + y2) % m

求是否存在t使得t时间后,h1 == a1 && h2 == a2。若存在求t最小值,否则输出 -1。
思路:因为对m取余,则可以联想到最后求的答案可能存在周期性。设t1为h1 == a1的首次时间,每隔p1时间后h1 == a1 。同理设t2为h2 == a2的首次时间,每隔p2时间后h2 == a2。可以先用暴力在m次内求出t1,p1,t2,p2。若求不出,则表示不可能。若求出,则依次相加求解,直到t1 == t2结束。

所有的数字都应该设为long long
最后依次相加求解是循环应为mod * 2

#include <cstdio>#include <cstring>#include <algorithm>#include <iostream>using namespace std;int mod;long long x1, x2, y1, y2, h1, h2, a1, a2;int main() {    while (scanf("%d%lld%lld%lld%lld%lld%lld%lld%lld", &mod, &h1, &a1, &x1, &y1, &h2, &a2, &x2, &y2) != EOF) {        long long t1 = -1, t2 = -1, p1 = -1, p2 = -1;//      求t1        for (int i = 1; i <= mod; i++) {            h1 = ((x1 * h1) % mod + y1) % mod;            if (h1 == a1) {                t1 = i;                break;            }        }//      t1不存在        if (t1 == -1) {            printf("-1\n");            return 0;        }//      求p1        for (int i = 1; i <= mod; i++) {            h1 = ((x1 * h1) % mod + y1) % mod;            if (h1 == a1) {                p1 = i;                break;            }        }//      求t2        for (int i = 1; i <= mod; i++) {            h2 = ((x2 * h2) % mod + y2) % mod;            if (h2 == a2) {                t2 = i;                break;            }        }//      t2不存在        if (t2 == -1) {            printf("-1\n");            return 0;        }//      求p2        for (int i = 1; i <= mod; i++) {            h2 = ((x2 * h2) % mod + y2) % mod;            if (h2 == a2) {                p2 = i;                break;            }        }//      求相同时间达到最后结果        for (int i = 1; i <= mod * 2; i++) {            if (t1 == t2) {                printf("%lld\n", t1);                return 0;            }            if (t1 < t2)                 t1 += p1;            else                 t2 += p2;        }        printf("-1\n");    }    return 0;}
0 0
原创粉丝点击