campjls讲过模数循环节的问题,今天做cf才做到这类题

h1->a1的长度为len1,a1->a1的长度为cir1

h2->a2的长度为len2,a2->a2的长度为cir2

要注意特判,再用exgcd求

len1+cir1*t1 = len2+cir2*t2的一组整数解,把t1回代就是答案

//#pragma comment(linker,"/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
#include <stack>
#include <list>
using namespace std;
const int SZ=,INF=0x7FFFFFFF;
typedef long long lon; lon exgcd(lon a,lon b,lon &x,lon &y,lon &d)
{
if(b==)
{
x=;
y=;
d=a;
return a;
}
lon gcd=exgcd(b,a%b,x,y,d);
lon x2=x,y2=y;
x=y2;
y=x2-(a/b)*y2;
return gcd;
} int main()
{
std::ios::sync_with_stdio();
//freopen("d:\\1.txt","r",stdin);
lon m;
cin>>m;
lon h1,a1,x1,y1,h2,a2,x2,y2;
cin>>h1>>a1>>x1>>y1>>h2>>a2>>x2>>y2;
set<pair<lon,lon>> st;
st.insert(make_pair(h1,h2));
lon cnt1=,cnt2=,pri1=,pri2=,t1=,t2=;
for(lon time=;;++time)
{
lon newa1=(x1*h1+y1)%m,newa2=(x2*h2+y2)%m;
if(newa1==a1)
{
if(cnt1==)pri1=time;
else if(cnt1==) t1=time-pri1;
++cnt1;
}
if(newa2==a2)
{
if(cnt2==)pri2=time;
else if(cnt2==)t2=time-pri2;
++cnt2;
}
if(pri1&&pri2&&t1&&t2)break; if(newa1==a1&&newa2==a2)
{
cout<<time<<endl;
return ;
}
if(time>1e7)
{
cout<<-<<endl;
return ;
}
h1=newa1,h2=newa2;
}
lon x,y,d;
exgcd(abs(t1),abs(t2),x,y,d);
if(abs(pri1-pri2)%d==)
{
x*=(pri2-pri1)/d;
y*=(pri2-pri1)/d;
y*=-;
if(x<)
{
int beishu=abs((x)/(t2/d));
x=x+beishu*abs(t2/d);
y=y+beishu*abs(t1/d);
}
if(y<)
{
int num=;
num=abs((y)/(t1/d));
y=y+num*abs(t1/d);
if(y<)
{
++num;
y+=abs(t1/d);
}
x+=num*abs(t2/d);
}
if(x<)x+=abs(t2/d);
if(x>&&y>)
{
int num1=abs((x)/(t2/d));
int num2=abs((y)/(t1/d));
int miner=min(num1,num2);
x-=miner*abs(t2/d);
y-=miner*abs(t1/d);
}
cout<<pri1+x*t1<<endl;
}
else cout<<-<<endl;
return ;
}

模数循环节——cf547A的更多相关文章

  1. HDU 5895 Mathematician QSC(矩阵乘法+循环节降幂+除法取模小技巧+快速幂)

    传送门:HDU 5895 Mathematician QSC 这是一篇很好的题解,我想讲的他基本都讲了http://blog.csdn.net/queuelovestack/article/detai ...

  2. 特征根法求通项+广义Fibonacci数列找循环节 - HDU 5451 Best Solver

    Best Solver Problem's Link Mean: 给出x和M,求:(5+2√6)^(1+2x)的值.x<2^32,M<=46337. analyse: 这题需要用到高中的数 ...

  3. 2019牛客暑期多校训练营(第九场)The power of Fibonacci——循环节&&CRT

    题意 求 $\displaystyle \sum_{i=1}^n F_i^m $,($1 \leq n\leq 10^9,1 \leq  m\leq 10^3$),答案对 $10^9$ 取模. 分析 ...

  4. hdu 2837 Calculation 指数循环节套路题

    Calculation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  5. HDU 3746 (KMP求最小循环节) Cyclic Nacklace

    题意: 给出一个字符串,要求在后面添加最少的字符是的新串是循环的,且至少有两个循环节.输出最少需要添加字符的个数. 分析: 假设所给字符串为p[0...l-1],其长度为l 有这样一个结论: 这个串的 ...

  6. Period(KMP,循环节问题)

    题意: 求给你个串,前i位子串由某个字符串重复k次得到,求所有的i和k 分析: i-next[i]恰好是一个循环节 #include <map> #include <set> ...

  7. uva202:循环小数(循环节+抽屉原理)

    题意: 给出两个数n,m,0<=n,m<=3000,输出n/m的循环小数表示以及循环节长度. 思路: 设立一个r[]数组记录循环小数,u[]记录每次的count,用于标记,小数计算可用 r ...

  8. poj 1961 Period【求前缀的长度,以及其中最小循环节的循环次数】

    Period Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 14653   Accepted: 6965 Descripti ...

  9. [KMP求最小循环节][HDU3746][Cyclic Nacklace]

    题意 给你个字符串,问在字符串末尾还要添加几个字符,使得字符串循环2次以上. 解法 无论这个串是不是循环串 i-next[i] 都能求出它的最小循环节 代码: /* 思路:kmp+字符串的最小循环节问 ...

随机推荐

  1. Scrapy爬虫框架的使用

    #_author:来童星#date:2019/12/24# Scrapy爬虫框架的使用#1.安装Twisted模块 https://www.lfd.uci.edu/~gohlke/pythonlibs ...

  2. table 中 当前行变量的获取

  3. Python sqlite3.ProgrammingError: You must not use 8-bit bytestrings unless you use a text_factory that can interpret 8-bit bytestrings......

    完整的错误信息如下: You must not use 8-bit bytestrings unless you use a text _factory that can interpret 8-bi ...

  4. Openstack组件部署 — Keystone Install & Create service entity and API endpoints

    目录 目录 前文列表 Install and configure Prerequisites 先决条件 Create the database for identity service 生成一个随机数 ...

  5. 用iptables实现代理上网

    环境:内网:eth1:192.168.2.0/24外网:eth0:10.17.0.111用iptables实现NATSNAT:改变数据包的源地址.防火墙会使用外部地址,替换数据包的本地网络地址.这样使 ...

  6. 将日志(Microsoft.Extensions.Logging)添加到.NET Core控制台应用程序

    在.NET Core项目中,日志记录是通过依赖项注入进行管理的. 尽管这对于ASP.NET项目效果很好,但在启动Startup.cs中的新项目时,所有这些都会自动创建,而在控制台应用程序中则需要一些配 ...

  7. memcached 安装与简单实用使用

    一.简介 1.memcache与memcached的区别与联系: memcache是php的拓展,memcached是客户端,复杂的说:Memcache模块提供了于memcached方便的面向过程及面 ...

  8. 11. Django及数据库环境搭建

    项目背景 我们做功能测试的时候知道,一个产品基本上都会有增删改查功能,对应接口也就是增删改查接口.做了2年接口自动化测试,觉得最难的点应该是数据的初始化,其实测试一个接口.对接口使用单元测试框架.生成 ...

  9. 使用latex绘制多层神经网络结构图

    1,使用Tikz包: 2,参考官方例程单层神经网络结构,绘制了一个含有3隐藏层的BP神经网络节点图 代码如下: \documentclass{article} \usepackage{tikz} \b ...

  10. 搭建RAID10(5块硬盘)过程并模拟其中一块硬盘损坏

    首先:RAID 10,实际是将RAID 0和RAID 1标准结合的产物,在连续地以位或字节为单位分割数据并且并行读/写多个磁盘的同时,为每一块磁盘作磁盘镜像进行冗余.它的优点是同时拥有RAID 0的超 ...