zoukankan      html  css  js  c++  java
  • 数论/暴力 Codeforces Round #305 (Div. 2) C. Mike and Frog

    题目传送门

     1 /*
     2     数论/暴力:找出第一次到a1,a2的次数,再找到完整周期p1,p2,然后以2*m为范围
     3                 t1,t2为各自起点开始“赛跑”,谁落后谁加一个周期,等到t1 == t2结束
     4     详细解释:http://blog.csdn.net/u014357885/article/details/46044287
     5 */
     6 #include <cstdio>
     7 #include <algorithm>
     8 #include <cstring>
     9 #include <iostream>
    10 using namespace std;
    11 
    12 typedef long long ll;
    13 
    14 const int MAXN = 1e3 + 10;
    15 const int INF = 0x3f3f3f3f;
    16 
    17 int main(void)        //Codeforces Round #305 (Div. 2) C. Mike and Frog
    18 {
    19     int m;
    20     ll h1, a1, x1, y1, h2, a2, x2, y2;
    21     while (scanf ("%d%I64d%I64d%I64d%I64d%I64d%I64d%I64d%I64d", &m, &h1, &a1, &x1, &y1, &h2, &a2, &x2, &y2) == 9)
    22     {
    23         ll t1 = -1, t2 = -1, p1 = -1, p2 = -1;
    24         for (int i=1; i<=m; ++i)                //get t1, p1
    25         {
    26             h1 = (h1 * x1 + y1) % m;
    27             if (h1 == a1)    {t1 = i; break;}
    28         }
    29         if (t1 == -1)    {puts ("-1"); continue;}
    30         for (int i=1; i<=m; ++i)
    31         {
    32             h1 = (h1 * x1 + y1) % m;
    33             if (h1 == a1)    {p1 = i; break;}
    34         }
    35 
    36         for (int i=1; i<=m; ++i)                //get t2, p2
    37         {
    38             h2 = (h2 * x2 + y2) % m;
    39             if (h2 == a2)    {t2 = i; break;}
    40         }
    41         if (t2 == -1)    {puts ("-1"); continue;}
    42         for (int i=1; i<=m; ++i)
    43         {
    44             h2 = (h2 * x2 + y2) % m;
    45             if (h2 == a2)    {p2 = i; break;}
    46         }
    47 
    48         bool ok = false;                        //get t1 == t2
    49         for (int i=1; i<=2*m; ++i)
    50         {
    51             if (t1 == t2)    {ok = true; printf ("%I64d
    ", t1);    break;}
    52             if (t1 < t2)    t1 += p1;
    53             else    t2 += p2;
    54         }
    55         if (!ok)    puts ("-1");
    56     }
    57 
    58     return 0;
    59 }
    60 
    61 /*
    62 5
    63 4 2
    64 1 1
    65 0 1
    66 2 3
    67 1023
    68 1 2
    69 1 0
    70 1 2
    71 1 1
    72 */
    编译人生,运行世界!
  • 相关阅读:
    虚拟机里的mysql怎么外连
    Pytest跳过执行之@pytest.mark.skip()详解大全
    判断字符是什么,返回True或者False
    s = "ajldjlajfdljfddd",去重并从小到大排序输出"adfjl"(sort与sorted、reverse与reversed的区别)
    1、输入一个姓名,判断是否姓王 2、strip和replace的用法
    python 运算符
    控制语句--while循环
    控制语句-if
    函数
    控制语句--for循环
  • 原文地址:https://www.cnblogs.com/Running-Time/p/4534183.html
Copyright © 2011-2022 走看看