CF 345A Mike and Frog

题目

自己歪歪的做法WA了好多发。

原题中每一秒都相当于

x1 = f1(x1)

x2 = f2(x2)

然后这是一个定义域和值域都在[0,m-1]的函数,显而易见其会形成一个环。

而且环长不超过m,所以实际上问题就分为了两部分:

1.x1变到a1,x2变到a2(h -> a的长度)

2.x1做循环,x2做循环直到同时为a1,a2。(a -> a的长度)

我们设第一步分别用了m1 s, m2 s,第二步用了 t1 s ,t2 s。

对于第一部分我们可以直接枚举吗,因为长度不超过m。

对于第二部分实际上是两个式子。

ans =  △1 * t1 + m1

ans =  △2 * t2 + m2

这个式子只需要枚举就行了。因为m1,m2最多相差不超过m,而在最优决策下△1或△2绝对值每变化1,m1,m2必然至少变化1,所以△最大为m

问题解决,注意各种特判,比如都没有t值,有一个有t值,无解的情况。

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #include <cstdlib>
 5
 6 #define LL long long
 7
 8 using namespace std;
 9
10 LL m,ans;
11
12 LL gcd(LL a,LL b){
13     if(!b) return a;
14     return gcd(b,a%b);
15 }
16
17 /*
18 since the f(x) be an fuction from a num x to y.
19 so it may be a cricle.
20 */
21
22 void solve(LL &ansv,LL &sumv){
23     LL h,a,x,y,ans=0;
24     scanf("%I64d%I64d%I64d%I64d",&h,&a,&x,&y);
25     for(int i=1;i<=m;i++){
26         h=(h*x%m+y)%m;
27         if(h==a){
28             ansv=(LL)i;    //how many seconds it would take for us to arrive ‘a‘ from ‘h‘
29             goto L;
30         }
31     }
32     puts("-1");
33     exit(0);
34     L:h=a;
35     sumv=-1;
36     for(int i=1;i<=m;i++){
37         h=(h*x%m+y)%m;    //how many seconds it would take for us to arrive ‘a‘ from ‘a‘
38         if(h==a){
39             sumv=(LL)i;
40             return;
41         }
42     }
43 }
44
45 /*
46 a1 + k1*a2 = b1 + k2*b2
47
48 ans = a1 (mod a2)
49 ans = b1 (mod b2)
50 */
51
52 int main(){
53     scanf("%I64d",&m);
54     LL a1,a2,b1,b2;
55     solve(a1,a2);
56     solve(b1,b2);
57     if(a1==b1) ans=a1;
58     else if(a2==-1&&b2==-1){
59         puts("-1");
60         return 0;
61     }
62     else if(a2==-1&&a1>b1&&(a1-b1)%b2==0) ans=a1;
63     else if(b2==-1&&b1>a1&&(b1-a1)%a2==0) ans=b1;
64     else if(a2==-1||b2==-1){
65         puts("-1");
66         return 0;
67     }
68     else{
69         LL k1;
70         for(k1=0;k1<=m;k1++)
71             if((a1+k1*a2-b1)%b2==0){
72                 ans=a1+k1*a2;
73                 if(ans>=b1) break;
74             }
75         if(k1>m) ans=-1;
76     }
77     printf("%I64d\n",ans);
78     return 0;
79 }
时间: 2025-01-15 20:29:43

CF 345A Mike and Frog的相关文章

数论/暴力 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 #in

Codeforces548C:Mike and Frog

Mike has a frog and a flower. His frog is named Xaniar and his flower is named Abol. Initially(at time 0), height of Xaniar is h1 and height of Abol is h2. Each second, Mike waters Abol and Xaniar. So, if height of Xaniar is h1 and height of Abol is 

Codeforces Round #305 (Div. 2) C. Mike and Frog +B. Mike and Fun

Mike has a frog and a flower. His frog is named Xaniar and his flower is named Abol. Initially(at time 0), height of Xaniar is h1 and height of Abol is h2. Each second, Mike waters Abol and Xaniar. So, if height of Xaniar is h1 and height of Abol is 

CF 548B Mike and Fun

Descripe Mike and some bears are playing a game just for fun. Mike is the judge. All bears except Mike are standing in an n × m grid, there's exactly one bear in each cell. We denote the bear standing in column number j of row number i by (i, j). Mik

CF 548A Mike and Fax

Descripe While Mike was walking in the subway, all the stuff in his back-bag dropped on the ground. There were several fax messages among them. He concatenated these strings in some order and now he has string s. He is not sure if this is his own bac

codeforces #305 A Mike and Frog

挺简单的题目,但是有一堆恶心的边界 在刨去恶心的边界之后: 假定我们知道两边的循环节为b1,b2 其中h第一次到达目标的时间为a1,a2 又知道对于答案t t=a1+b1*t1=a2+b2*t2 不妨枚举t1,判断是否存在可行解即可 又因为LCM(b1,b2)就开始循环了 且b1*b2<=b1*mod 所以我们枚举t1的范围在[0,mod]即可 如果在这个范围内无解,则一定无解 #include<cstdio> #include<cstdlib> #include<cs

cf 547B. Mike and Feet dp

题意: n个矩阵排成一排,n<=2e5,高度分别为hei[i],宽度为1 对于一些连续的矩阵,矩阵的size为矩阵的个数,矩阵的strength为这些矩阵中高度最低的那一个高度 求:for each x such that 1 ≤ x ≤ n the maximum strength among all groups of size x. 对于每一个矩阵,我们先求出这个矩阵的l,r l表示这个矩阵左边最靠近它的小于它的矩阵的下标 r表示这个矩阵右边最靠近它的小于它的矩阵的下标 即在区间(l,r)

#305 (div.2) C. Mike and Frog

1.题目描述:点击打开链接 2.解题思路:本题是一道模拟题,虽然看上去像数学,但实际上只需要模拟一下这个过程就好了.首先,我们先让h1变为a1,设需要k1步才可以,如果h2经过k1步也变为a2,那么直接输出答案.否则,我们接下来找h1变化的循环节cycle,即每经过一个cycle,就可以再到达a1.那么我们看h2需要几个cycle才可以从当前位置(即第k1步时候的位置)到达a2,假设只需要k2步,那么最终的答案就是k2*cycle+k1. 本题的细节比较多,详细过程还需要见注释部分好好理解. 3

codeforces 547A Mike and Frog

最近都是这种题呢...... 哎 开始想纯暴力(体现在跳出循环t>=那里,,,,),,,,随着数据变大.....(t=499981500166是可以的),,,,,,,23333333 超时代码: #include <iostream> #include<bits/stdc++.h> using namespace std; int main() { long long int mod; scanf("%lld",&mod); long long i