近期都是这样的题呢。

。。。。

開始想纯暴力(体如今跳出循环t>=那里。,,,)。。,。随着数据变大。。

。。。(t=499981500166是能够的),,,。。,,23333333

超时代码:

#include <iostream>

#include<bits/stdc++.h>

using namespace std;

int main()

{

    long long int mod;

    scanf("%lld",&mod);

    long long int yuanshi1,mubiao1;

    scanf("%lld%lld",&yuanshi1,&mubiao1);

    long long int x1,y1;

    scanf("%lld%lld",&x1,&y1);

    long long int yuanshi2,mubiao2;

    scanf("%lld%lld",&yuanshi2,&mubiao2);

    long long int x2,y2;

    scanf("%lld%lld",&x2,&y2);

    long long int t=0;

    while(yuanshi1!=mubiao1||yuanshi2!=mubiao2)

    {

        yuanshi1=(x1*yuanshi1+y1)%mod;

        yuanshi2=(x2*yuanshi2+y2)%mod;

        t++;

        if(t>=50000000000)

        {

            printf("-1\n");

            return 0;

        }

    }

    printf("%lld\n",t);

    return 0;

}

非常质朴啊。。。。

此题最深刻的教训是。

。。。以后别对变量瞎起名字!

!!

!!

#include <iostream>

#include<bits/stdc++.h>

using namespace std;

int main()

{

    long long int mod;

    scanf("%lld",&mod);

    long long int yuanshi1,mubiao1;

    scanf("%lld%lld",&yuanshi1,&mubiao1);

    long long int x1,y1;

    scanf("%lld%lld",&x1,&y1);

    long long int yuanshi2,mubiao2;

    scanf("%lld%lld",&yuanshi2,&mubiao2);

    long long int x2,y2;

    scanf("%lld%lld",&x2,&y2);

    long long int k1=0,k2=0;

    while(yuanshi1!=mubiao1&&k1<mod)  //先算第一个满足的情况

    {

        yuanshi1=(x1*yuanshi1+y1)%mod;

        yuanshi2=(x2*yuanshi2+y2)%mod;

        k1++;

    }

    if(yuanshi1==mubiao1&&yuanshi2==mubiao2)    //同一时候第二个也满足了

    {

        printf("%lld\n",k1);

        return 0;

    }

    if(yuanshi1!=mubiao1)        //假设跳出是由于大于了MOD那就没招了(题里给的!



    {

        printf("-1\n");

        return 0;

    }

    long long int c=1;

    yuanshi1=(yuanshi1*x1+y1)%mod;

    long long int x=x2,y=y2;

        while(yuanshi1!=mubiao1&&c<=mod)    //找两个循环的最小公倍数。。。。

。。

        {

            yuanshi1=(yuanshi1*x1+y1)%mod;

            x=(x*x2)%mod;

            y=(x2*y+y2)%mod;

            c++;

        }

        if(yuanshi1!=mubiao1)

        {

            printf("-1\n");

            return 0;

        }

        while(yuanshi2!=mubiao2&&k2<=mod){

            yuanshi2=(yuanshi2*x+y)%mod;

            k2++;

        }

        if(yuanshi2!=mubiao2){

            printf("-1\n");

            return 0;

        }

        printf("%lld\n",k2*c+k1);

    return 0;

}

还可以用拓展欧几里得。。

。。。

假设这个线性方程有解,那么一定有gcd(a,b) | m。即a。b的最大公约数可以整除m(m%gcd(a,b)==0)。



codeforces 547A Mike and Frog的更多相关文章

  1. Codeforces Round #305 (Div. 1) A. Mike and Frog 暴力

     A. Mike and Frog Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/547/pr ...

  2. 数论/暴力 Codeforces Round #305 (Div. 2) C. Mike and Frog

    题目传送门 /* 数论/暴力:找出第一次到a1,a2的次数,再找到完整周期p1,p2,然后以2*m为范围 t1,t2为各自起点开始“赛跑”,谁落后谁加一个周期,等到t1 == t2结束 详细解释:ht ...

  3. CF #305 (Div. 2) C. Mike and Frog(扩展欧几里得&&当然暴力is also no problem)

    C. Mike and Frog time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

  4. hdu4135-Co-prime & Codeforces 547C Mike and Foam (容斥原理)

    hdu4135 求[L,R]范围内与N互质的数的个数. 分别求[1,L]和[1,R]和n互质的个数,求差. 利用容斥原理求解. 二进制枚举每一种质数的组合,奇加偶减. #include <bit ...

  5. codeforces 547E Mike and Friends

    codeforces 547E Mike and Friends 题意 题解 代码 #include<bits/stdc++.h> using namespace std; #define ...

  6. codeforces 689 Mike and Shortcuts(最短路)

    codeforces 689 Mike and Shortcuts(最短路) 原题 任意两点的距离是序号差,那么相邻点之间建边即可,同时加上题目提供的边 跑一遍dijkstra可得1点到每个点的最短路 ...

  7. codeforces #305 A Mike and Frog

    挺简单的题目,但是有一堆恶心的边界 在刨去恶心的边界之后: 假定我们知道两边的循环节为b1,b2 其中h第一次到达目标的时间为a1,a2 又知道对于答案t t=a1+b1*t1=a2+b2*t2 不妨 ...

  8. Codeforces 548B Mike and Fun

    传送门 B. Mike and Fun time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  9. Codeforces 798D Mike and distribution - 贪心

    Mike has always been thinking about the harshness of social inequality. He's so obsessed with it tha ...

随机推荐

  1. 在Mac OS X中使用mtr诊断路由节点问题

    这个工具是从阿里云客服那知道的,当时遇到阿里云CDN的一个节点出现丢包问题,用这个工具诊断路由节点问题. 1. 下载地址:http://rudix.org/packages/mtr.html(在园子里 ...

  2. 20个angularjs开发工具

    AngularJS是那些渴望以动态方式来设计web app的web开发人员最偏爱的框架之一.如果你是一个希望启动AngularJS项目的开发人员,那么你可能需要帮助来挑选出趁手的工具…… 在Value ...

  3. ASP.NET(C#)不提示直接关闭当前页面

    protected void Button1_Click(object sender, EventArgs e) { //关闭页面--要弹出提示(IE6及以下不弹出提示) ClientScript.R ...

  4. ffmpeg 批量转换swf为mp3

    下了几个音乐居然都是swf格式,在网上找了一下没找到好用的转换工具,于是想到了ffmpeg. linux下可以直接安装ffmpeg #/bin/sh for f in *.swf do ffmpeg ...

  5. 【C#】BackgroundWorker类传递参数

    要传递多个不同类型的参数: private void Test(){ int a = 123; string b = "456"; bool c = true; Backgroun ...

  6. C语言 · 因式分解

    算法提高 8-1因式分解   时间限制:10.0s   内存限制:256.0MB      问题描述 设计算法,用户输入合数,程序输出若个素数的乘积.例如,输入6,输出2*3.输入20,输出2*2*5 ...

  7. DataGridView使用技巧三:不显示最下面的新行、判断新增行

    一.DataGridView不显示下面的新行 通常DataGridView的最下面一行是用户新追加的行(行头显示*).如果不想让用户新追加行即不想显示该新行,可以将DataGridView对象的All ...

  8. Specified VM install not found: type Standard VM, name jdk1.6...

    运行ant的时候出现 Specified VM install not found: type Standard VM, name.... 搞了好久..汗汗 尝试删除这些文件: ... / .meta ...

  9. Error -27780: Connection reset by peer: socket write error

    Problem Description: Error: "-27780: read to host failed: [10054] Connection reset by peer" ...

  10. [oracle] 两种权限:系统权限VS对象权限

    系统权限表示对表和表空间等   有无操作权  的权限.一般是SYS用户这种DBA来授权.比如: grant create session to lisi grant create table to l ...