ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Codeforces Round #305 (Div. 1) A Mike and Frog 循环节,EXCRT

2020-07-24 09:31:57  阅读:261  来源: 互联网

标签:Mike return res ll 305 Frog a1 include define


 

 此题主要学到了处理循环节的问题。

对于每个生物而言,有可能有无数能到a1,有可能仅1次到a1,也可能一次也到不了a1

对于一次也到不了,直接输出-1

对于仅1次跑到a1,for m次,如果没有就没有了

对于无数次,联立同余方程,可以用EXGCD或者EXCRT求解。

#pragma warning(disable:4996)

#include<iostream>
#include<algorithm>
#include<bitset>
#include<tuple>
#include<unordered_map>
#include<fstream>
#include<iomanip>
#include<string>
#include<cmath>
#include<cstring>
#include<vector>
#include<map>
#include<set>
#include<list>
#include<queue>
#include<stack>
#include<sstream>
#include<cstdio>
#include<ctime>
#include<cstdlib>
#define INF 0x3f3f3f3f
#define inf 0x7FFFFFFF
#define MOD 1000000007
#define moD 1000000003
#define pii pair<int,string>
#define eps 1e-8
#define equals(a,b) (fabs(a-b)<eps)
#define bug puts("bug")
#define re  register
#define fi first
#define se second
const int maxn = 1e6 + 5;
const double Inf = 10000.0;
const double PI = acos(-1.0);
typedef  long long ll;
typedef unsigned long long ull;
using namespace std;

int f1, f2;
int n;
vector<ll> v1, v2;



ll ai[maxn], bi[maxn];

ll mul(ll a, ll b, ll mod)
{
    ll res = 0;
    while (b > 0)
    {
        if (b & 1) res = (res + a) % mod;
        a = (a + a) % mod;
        b >>= 1;
    }
    return res;
}

ll exgcd(ll a, ll b, ll& x, ll& y)
{
    if (b == 0) { x = 1; y = 0; return a; }
    ll gcd = exgcd(b, a % b, x, y);
    ll tp = x;
    x = y; y = tp - a / b * y;
    return gcd;
}

ll excrt()
{
    ll x, y, k;
    ll M = bi[1], ans = ai[1];
    for (int i = 2; i <= n; i++)
    {
        ll a = M, b = bi[i], c = (ai[i] - ans % b + b) % b;
        ll gcd = exgcd(a, b, x, y), bg = b / gcd;
        if (c % gcd != 0) return -1;

        x = mul(x, c / gcd, bg);
        ans += x * M;
        M *= bg;
        ans = (ans % M + M) % M;
    }
    return (ans % M + M) % M;
}


int main() {
    ll m, h1, a1, h2, a2, x1, y1, x2, y2;
    int pos1, pos2;
    scanf("%lld", &m);
    ll tmp, s1, s2, p1, p2;
    scanf("%lld %lld %lld %lld %lld %lld %lld %lld", &h1, &a1, &x1, &y1, &h2, &a2, &x2, &y2);
    tmp = h1;
    v1.push_back(tmp);
    for (int i = 1; i < 2 * m; i++) {
        tmp = (x1 * tmp + y1) % m;
        v1.push_back(tmp);
    }
    for (int i = 0; i < 2 * m; i++) {
        if (v1[i] == a1) {
            f1++;
            if (f1 == 1) s1 = i;
            if (f1 == 2) {
                p1 = i - s1;
                break;
            }
        }
    }
    if (f1 != 2) p1 = 0;
    tmp = h2;
    v2.push_back(tmp);
    for (int i = 1; i < 2 * m; i++) {
        tmp = (x2 * tmp + y2) % m;
        v2.push_back(tmp);
    }
    for (int i = 0; i < 2 * m; i++) {
        if (v2[i] == a2) {
            f2++;
            if (f2 == 1) s2 = i;
            if (f2 == 2) {
                p2 = i - s2;
                break;
            }
        }
    }
    if (f2 != 2) p2 = 0;
    if (!p2 || !p2) {
        for (int i = 0; i < m; i++) {
            if (v1[i] == a1 && v2[i] == a2) {
                printf("%d", i);
                return 0;
            }
        }
        puts("-1");
        return 0;
    }
    if (!f1 || !f2) {
        puts("-1");
        return 0;
    }
    for (int i = 0; i < m; i++) {
        if (v1[i] == a1 && v2[i] == a2) {
            printf("%d", i);
            return 0;
        }
    }
    n = 2;
    bi[1] = p1 , bi[2] = p2;
    ai[1] = s1 , ai[2] = s2;
    ll res = excrt();
    if (res == -1) puts("-1");
    else printf("%lld", res);
}

 

标签:Mike,return,res,ll,305,Frog,a1,include,define
来源: https://www.cnblogs.com/hznumqf/p/13370152.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有