近期都是这样的题呢。

。。。。

開始想纯暴力(体如今跳出循环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. Object.defineProperty()属性设置介绍

    注释: 本文宅自https://segmentfault.com/a/1190000007434923,仅学习方便,没有任何商业用途 对象是由多个名/值对组成的无序的集合.对象中每个属性对应任意类型的 ...

  2. Ubuntu12.04下apache服务器的安装也配置

    安装步骤 1.下载httpd http://httpd.apache.org/ 2.解压缩httpd tar vxzf httpd-2.4.6.tar.gz 3.安装httpd 进入解压缩后的目录 c ...

  3. NAND FLASH驱动程序

    NAND FLASH是一个存储芯片那么: 这样的操作很合理"读地址A的数据,把数据B写到地址A" 问1. 原理图上NAND FLASH和S3C2440之间只有数据线,     怎么 ...

  4. xslt循环转换子元素

    转换源xml <keywords class="array"> <e type="string">e1</e> <e ...

  5. 【转】Java对日期Date类进行加减运算,年份加减,月份加减

    import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date; public class Date ...

  6. 在jsp里面不要瞎用<!-- -->注释

    如图: SEVERE: Servlet.service() for servlet jsp threw exceptionorg.apache.jasper.JasperException: /ch1 ...

  7. NYOJ-451-光棍节的快乐

    光棍节的快乐 时间限制:1000 ms  |  内存限制:65535 KB 难度: 描写叙述 光棍们,今天是光棍节.聪明的NS想到了一个活动来丰富这个光棍节. 规则例如以下: 每一个光棍在一个纸条上写 ...

  8. MySQL命令学习(二)

    (13)where字句操作符 =            等于 <>          不等于 !=           不等于 <            小于 >        ...

  9. 我的开源主页Blog Lite配置指南

    JinHengyu.github.io --- Blog Lite 0.1.1 好看的东西看多了就会不好看, 简单的东西永远不会难看 GitHub Pages 提供静态网站托管服务的厂商还是很多的, ...

  10. jqueryEasyui常用代码

    //查询: function doSearch(form){ var fields =$('#queryForm').serializeArray(); var $fm = $(form); var ...