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 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的更多相关文章
- 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 ...
- 数论/暴力 Codeforces Round #305 (Div. 2) C. Mike and Frog
题目传送门 /* 数论/暴力:找出第一次到a1,a2的次数,再找到完整周期p1,p2,然后以2*m为范围 t1,t2为各自起点开始“赛跑”,谁落后谁加一个周期,等到t1 == t2结束 详细解释:ht ...
- 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 ...
- hdu4135-Co-prime & Codeforces 547C Mike and Foam (容斥原理)
hdu4135 求[L,R]范围内与N互质的数的个数. 分别求[1,L]和[1,R]和n互质的个数,求差. 利用容斥原理求解. 二进制枚举每一种质数的组合,奇加偶减. #include <bit ...
- codeforces 547E Mike and Friends
codeforces 547E Mike and Friends 题意 题解 代码 #include<bits/stdc++.h> using namespace std; #define ...
- codeforces 689 Mike and Shortcuts(最短路)
codeforces 689 Mike and Shortcuts(最短路) 原题 任意两点的距离是序号差,那么相邻点之间建边即可,同时加上题目提供的边 跑一遍dijkstra可得1点到每个点的最短路 ...
- codeforces #305 A Mike and Frog
挺简单的题目,但是有一堆恶心的边界 在刨去恶心的边界之后: 假定我们知道两边的循环节为b1,b2 其中h第一次到达目标的时间为a1,a2 又知道对于答案t t=a1+b1*t1=a2+b2*t2 不妨 ...
- Codeforces 548B Mike and Fun
传送门 B. Mike and Fun time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Codeforces 798D Mike and distribution - 贪心
Mike has always been thinking about the harshness of social inequality. He's so obsessed with it tha ...
随机推荐
- Spark学习笔记总结-超级经典总结
Spark简介 spark 可以很容易和yarn结合,直接调用HDFS.Hbase上面的数据,和hadoop结合.配置很容易. spark发展迅猛,框架比hadoop更加灵活实用.减少了延时处理,提高 ...
- 破解swf文件
但我们在网页上看到漂亮的flashs,想看下别人是怎么做的,怎么办? 答曰:我们需要两个工具 swf_reader_2.3 (把浏览器中的flash下载到本地的swf文件)下载地址http://dow ...
- HttpClient 教程 (四)
第四章 HTTP认证 HttpClient提供对由HTTP标准规范定义的认证模式的完全支持.HttpClient的认证框架可以扩展支持非标准的认证模式,比如NTLM和SPNEGO. 4.1 用户凭证 ...
- C语言一闪而过
头文件#include<stdlib.h> main函数system("pause");
- IOS之TableViewCell重用机制避免重复显示问题
常规配置如下 当超过tableView显示的范围的时候 后面显示的内容将会和前面重复. 1 // 这样配置的话超过页面显示的内容会重复出现 2 - (UITableViewCell *)tableVi ...
- oracle中普通视图和实体试图的区别
Oracle普通视图和实体化视图比较 来源:茂盛博客|2013-07-30 Oracle普通视图和实体化视图比较 相对于普通的视图来说,实体化视图的不同之处在于实体化视图管理存储数据,占据数据库的物理 ...
- 开源图形数据库Neo4j使用 php开发
先看看它的示例数据 打开 Neo4j Browser :play movie graph 写代码,然后点play执行 Cypher, the graph query language.Neo4j提供了 ...
- Connector/c++ 查询Mysql,出现 can't fetch because not on result set 错误
使用 Connector/C++ 查询 Mysql 时,出现错误,提示如下: can't fetch because not on result set, 出现原因可以看这里:http://stack ...
- JPA联合主键
联合主键也就是说需要多个字段才能确定数据库记录中的唯一一行.这样就需要多个字段一起,组成主键,也叫联合主键.例如飞机航线,我们需要知道飞机起飞的地点以及飞机降落的地点.所以需要飞机起飞的地点和降落的地 ...
- [oracle] oracle的三种密码验证机制以及在windows和linux下的不同启动过程
oracle数据库的密码验证机制: ① 操作系统验证 拥有SYSDBA和SYSOPER的用户用该方式验证此时数据库无需启动,也无需开启监听和实例服务. 要求:本地组ora_dba中有该操作系统的登录用 ...