bzoj1898: [Zjoi2004]Swamp 沼泽鳄鱼

矩阵乘法

每12个单位时间是个循环

前K/12个单位时间用快速幂,后K%12个单位时间用裸矩阵乘法

#include <cstdio>
#include <cstring>
#define MAXN 53
#define MOD 10000
int n, m, s, t, nf;
long T;
int a[12][MAXN][MAXN];
int b[MAXN][MAXN], tmp[MAXN][MAXN];
void multiply(int a[MAXN][MAXN], int b[MAXN][MAXN], int f[MAXN][MAXN])
{
    memset(f, 0, sizeof(int) * MAXN * MAXN);
    for (int i = 0; i < n; ++i)
        for (int j = 0; j < n; ++j)
            for (int k = 0; k < n; ++k)
                f[i][j] = (f[i][j] + a[i][k] * b[k][j] % MOD) % MOD;
}
void power(int a[MAXN][MAXN], int b, int f[MAXN][MAXN])
{
    if (b == 0)
    {
        memset(f, 0, sizeof(int) * MAXN * MAXN);
        for (int i = 0; i < n; ++i)
            f[i][i] = 1;
        return;
    }
    if (b == 1)
    {
        memcpy(f, a, sizeof(int) * MAXN * MAXN);
        return;
    }
    int t[MAXN][MAXN];
    power(a, b / 2, f);
    multiply(f, f, t);
    if (b % 2 == 0)
        memcpy(f, t, sizeof(int) * MAXN * MAXN);
    else
        multiply(t, a, f);
}
int main()
{
    scanf("%d%d%d%d%ld", &n, &m, &s, &t, &T);
    memset(a, 0, sizeof(a));
    while (m--)
    {
        int u, v;
        scanf("%d%d", &u, &v);
        for (int i = 0; i < 12; ++i)
            a[i][u][v] = a[i][v][u] = 1;
    }
    scanf("%d", &nf);
    while (nf--)
    {
        int l, x[4];
        scanf("%d", &l);
        for (int i = 0; i < l; ++i)
            scanf("%d", &x[i]);
        for (int i = 0; i < 12; ++i)
            for (int j = 0; j < n; ++j)
            {
                a[i][j][x[(i + 1) % l]] = 0;
                a[i][x[i % l]][j] = 0;
            }
    }
    memcpy(b, a[0], sizeof(b));
    for (int i = 1; i < 12; ++i)
    {
        multiply(b, a[i], tmp);
        memcpy(b, tmp, sizeof(b));
    }
    power(b, T / 12, tmp);
    memcpy(b, tmp, sizeof(b));
    for (int i = 0; i < T % 12; ++i)
    {
        multiply(b, a[i], tmp);
        memcpy(b, tmp, sizeof(b));
    }
    printf("%d\n", b[s][t]);
    return 0;
}


Swamp Things

09-18

DescriptionnnHugh F. Oh, in his never-ending quest to prove the existence of extraterrestrials, has gotten hold of a number of nighttime photographs taken by a research group that is examining glowing swamp gas. Hugh wants to see if any of the photos show, not swamp gas, but Little Grey Men in glowing suits. The photographs consist of bright dots appearing against a black background. Unfortunately, at the time the photos were taken, trains were travelling through the area (there is a train trestle over the swamp), and occasional lights from the train windows also appear in the photographs. Hugh, being a fastidious researcher, wants to eliminate these spots from the images. He can't tell from the photos exactly where the tracks are, or from what direction the photos were taken, but he knows that the tracks in that area are perfectly straight, so he's decided on the following approach: he will find the line with the maximum number of spots lying on it and, if there are four or more spots on the line, he will eliminate those points from his calculations, assuming that those are windows on the train. If two or more lines have the maximum number of points, Hugh will just randomly select one such set and delete it from the photo (he's not all that fastidious – after all, he believes in Little Grey Men). If there are fewer than four points lying along a common line, Hugh will assume that there is no train in the photograph and won't delete any points. Please write a program for him to process a set of photographs.nInputnnThere will be a series of test cases. Each test case is one photograph described by a line containing a positive integer n (<= 1000), the number of distinct spots in the photograph, followed by n lines containing the integer coordinates of the spots, one (x, y) pair per line. All coordinates are between 0 and 10000. The last photo description is followed by a line containing a zero, marking the end of the input. This line should not be processed.nOutputnnFor each test case, output the photo number followed by the number of points eliminated from the photograph. Imitate the sample output below.nSample Inputnn6n0 1n0 2n1 2n2 2n4 5n5 6n4n3 5n4 4n6 5n7 4n0nSample OutputnnPhoto 1: 4 points eliminatednPhoto 2: 0 points eliminated

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试