Cod sursa(job #1713590)

Utilizator akaprosAna Kapros akapros Data 5 iunie 2016 23:53:07
Problema Robotei Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.22 kb
#include <bits/stdc++.h>
#define maxM 1000002
#define maxN 1002
#define ll long long
using namespace std;
int n, m, X, Y, modx, mody, offx, offy, len;
ll ans[maxM], ox[maxN], oy[maxN], oX[maxN], oY[maxN];
int vis[maxN][maxN];
bool a[maxN][maxN];
void read()
{
    freopen("robotei.in", "r", stdin);
    scanf("%d %d %d %d %d %d %d %d", &n, &m, &X, &Y, &modx, &mody, &offx, &offy);
    vis[X][Y] = 1;
}
void solve()
{
    int i, j;
    int x = X, y = Y;
    while (!a[x][y])
    {
        ++ len;
        a[x][y] = 1;
        x = (1LL * x * x + 1LL * offx) % modx;
        y = (1LL * y * y + 1LL * offy) % mody;
    }
    if (x != X || y != Y)
        {
            ans[0] = 1LL * n * n - 1;
            ans[1] = 1;
            return ;
        }
    for (i = modx; i < n; ++ i)
        ++ ox[(1LL * i * i + 1LL * offx) % modx];
     for (i = 0; i < modx; ++ i)
        ++ oX[(1LL * i * i + 1LL * offx) % modx];
    for (i = mody; i < n; ++ i)
        ++ oy[(1LL * i * i + 1LL * offy) % mody];
    for (i = 0; i < mody; ++ i)
        ++ oY[(1LL * i * i + 1LL * offy) % mody];

    for (i = 0; i < modx; ++ i)
        for (j = 0; j < mody; ++ j)
    {
        x = i;
        y = j;
        int l = 0;
        while (!vis[x][y])
        {
            ++ l;
            vis[x][y] = -1;
            x = (1LL * x * x + 1LL * offx) % modx;
            y = (1LL * y * y + 1LL * offy) % mody;
        }
        if (vis[x][y] != -1)
        {
            l += vis[x][y];
        if (l - 1 <= m)
        {
            ++ ans[1 + (m - l + 1) / len];
            if (l <= m)
            ans[1 + (m - l) / len] += 1LL * ox[i] * (oy[j] + oY[j]) + oy[j] * oX[i];
        }else
        ans[1 + (m - l) / len] += 1LL * ox[i] * oy[j] + 1;
        }

        x = i; y = j;
        while (!vis[x][y] || vis[x][y] == -1)
        {
            vis[x][y] = l --;
            x = (1LL * x * x + 1LL * offx) % modx;
            y = (1LL * y * y + 1LL * offy) % mody;
        }
    }
}
void write()
{
    int i;
    freopen("robotei.out", "w", stdout);

    for (i = 0; i <= m; ++ i)
        if (ans[i] != 0)
    printf("%d %d\n", i, ans[i]);
}
int main()
{
    read();
    solve();
    write();
    return 0;
}