题目链接:http://codeforces.com/problemset/problem/722/F

------------------------------------------------------------------------------

首先根据 $k <= 40$  以及 $lcm(1...40)$ 在$long long$以内

可以意识到这题可以转化为求最大合法区间使得区间内的同余方程组合法

这个可以考虑用$exgcd$来做 并且也满足区间可合并的性质

对于一段连续区间 我们枚举左端点要找到最远的合法的右端点 可以先用$ST$表预处理后倍增查找

$($注意线段树/$ST$表要进行二分的时候不要真的进行二分 这样会多一个$log$ 要利用已经分割好的区间$)$

最后复杂度是$O(n * logn * exgcd$复杂度$)$

 #include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + , E = 2e5 + ;
int len[N], firste[N], nexte[E], v[E], w[E];
int n, lim, e, ans, m;
void build(int x, int y, int z)
{
nexte[++e] = firste[x];
firste[x] = e;
v[e] = y;
w[e] = z;
}
void exgcd(long long a,long long b,long long &d, long long &x, long long &y)
{
if(!b)
{
x = ;
y = ;
d = a;
}
else
{
exgcd(b, a % b, d, y, x);
y -= x * (a / b);
}
}
long long a[][N], b[][N];
long long mul(long long aa, long long bb, long long mod)
{
return (aa * bb - (long long)(aa / (long double) mod * bb + 1e-) * mod + mod) % mod ;
}
void solve(long long m1, long long b1, long long m2, long long b2, long long &m0, long long &b0)
{
if(m1 == - || m2 == -)
{
m0 = b0 = -;
return;
}
long long d, x, y;
exgcd(m1, m2, d, x, y);
if((b2 - b1) % d != )
m0 = b0 = -;
else
{
long long t = m2 / d;
//x = (x % t * ((b2 - b1) / d % t) % t + t) % t;
x = (mul(x, (b2 - b1) / d, t) + t) % t;
m0 = m1 * (m2 / d);
b0 = m1 * x + b1;
}
}
int main()
{
scanf("%d%d", &n, &lim);
int x;
for(int i = ; i <= n; ++i)
{
scanf("%d", &len[i]);
for(int j = ; j < len[i]; ++j)
{
scanf("%d", &x);
build(x, i, j);
}
}
int last;
for(int u = ; u <= lim; ++u)
{
ans = m = last = ;
for(int p = firste[u]; ; p = nexte[p])
{
if(p && (!m || v[p] == last - ))
{
++m;
a[][m] = len[v[p]];
b[][m] = w[p];
last = v[p];
continue;
}
else
{
if(m > ans)
{
int top = ;
for(int i = ; ( << i) <= m; ++i)
{
top = i;
for(int j = ; j + ( << i) - <= m; ++j)
solve(a[i - ][j], b[i - ][j], a[i - ][j + ( << (i - ))],
b[i - ][j + ( << (i - ))], a[i][j], b[i][j]);
}
for(int j = ; m - j + > ans; ++j)
{
int k = j + ;
long long nowa = a[][j], nowb = b[][j], tmpa, tmpb;
for(int i = top; i >= ; --i)
if(k + ( << i) - <= m)
{
solve(nowa, nowb, a[i][k], b[i][k], tmpa, tmpb);
if(tmpa != -)
{
k += ( << i);
nowa = tmpa;
nowb = tmpb;
}
}
ans = max(ans, k - j);
}
}
if(!p)
break;
m = ;
a[][m] = len[v[p]];
b[][m] = w[p];
last = v[p];
}
}
printf("%d\n", ans);
}
return ;
}

codeforces 722F - Cyclic Cipher的更多相关文章

  1. [codeforces 901E] Cyclic Cipher 循环卷积-Bluestein's Algorithm

    题目大意: 传送门 给两个数列${B_i}.{C_i}$,长度均为$n$,且${B_i}$循环移位线性无关,即不存在一组系数${X_i}$使得对于所有的$k$均有$\sum_{i=0}^{n-1} X ...

  2. CodeForces - 156C:Cipher (不错的DP)

    Sherlock Holmes found a mysterious correspondence of two VIPs and made up his mind to read it. But t ...

  3. Codeforces - 102222C - Caesar Cipher

    https://codeforc.es/gym/102222/my 好像在哪里见过这个东西?字符的左右移还是小心,注意在mod26范围内. #include<bits/stdc++.h> ...

  4. codeforces 708ALetter Cyclic Shift

    2019-05-18 09:51:19 加油,加油,fightting !!! https://www.cnblogs.com/ECJTUACM-873284962/p/6375011.html 全为 ...

  5. Codeforces Round #453 (Div. 1)

    Codeforces Round #453 (Div. 1) A. Hashing Trees 题目描述:给出一棵树的高度和每一层的节点数,问是否有两棵树都满足这个条件,若有,则输出这两棵树,否则输出 ...

  6. Codeforces Round #453

    Visiting a Friend Solution Coloring a Tree 自顶向下 Solution Hashing Trees 连续2层节点数都超过1时能异构 Solution GCD ...

  7. Codeforces Round #385 (Div. 2) A. Hongcow Learns the Cyclic Shift 水题

    A. Hongcow Learns the Cyclic Shift 题目连接: http://codeforces.com/contest/745/problem/A Description Hon ...

  8. Codeforces 708A Letters Cyclic Shift

    A. Letters Cyclic Shift time limit per test:1 second memory limit per test:256 megabytes input:stand ...

  9. Cyclic Components CodeForces - 977E(DFS)

    Cyclic Components CodeForces - 977E You are given an undirected graph consisting of nn vertices and  ...

随机推荐

  1. HNUSTOJ-1437 无题

    1437: 无题 时间限制: 1 Sec  内存限制: 128 MB提交: 268  解决: 45[提交][状态][讨论版] 题目描述 tc在玩一个很无聊的游戏:每一次电脑都会给一个长度不超过10^5 ...

  2. some problem

    CF1257F Make Them Similar $solution:$ 折半搜索后考虑如何维护两个数组的和,可以将 $A$ 中每个数减 $A_1$ ,$B$ 中每个数被减 $B_1$ ,$map$ ...

  3. Jmeter添加压力机

    名词解释: 主控机:启动Jmeter 的机器 负载机:为压力测试提供除主控机之外资源的机器 步骤: 1.先在其他的负载机(另外的电脑)上启动jmeter-server (jmeter-server.b ...

  4. Jmeter学习总结

    学习内容: 1.用户定义的变量 作用:多个地方使用同一个值,且该值在不同的环境下不同,方便脚本在不同环境下运行时修改. 2.基本的HTTP请求,请求方式:get 3.传入参数为json 4.HTTP信 ...

  5. SSM框架的常用注解整理

    一.mybatis 1 配置一对多查询和多对多查询的注解方式映射关系: @Results:声明映射关系的配置  Value属性接收 @Result的数组 @Result:配置映射关系   id属性(b ...

  6. 学习MyBatis时报的错

    初学MyBatis第一天跟着敲代码,一直报错,报错到崩溃,错误如下 org.apache.ibatis.exceptions.PersistenceException: ### Error query ...

  7. Docker介绍,安装和常用的命令

    Docker是Google公司推出的Go语言开发的,基于Linux内核的cgroup,namespace,AUFS类的UnionFS等技术.对进程进行封装格力,属于操作系统层面的虚拟化技术.隔离的进程 ...

  8. Manjaro系统和软件安装记录

    Linux桌面环境  ArchLinux官方wiki manjaro官方wiki pacman官方wiki 从www.distrowatch.com可以查看Linux发行版排行榜,可以看到manjar ...

  9. SpringBoot之初体验

    一.SpringBoot 介绍 1.1 SpringBoot 使命 在之前我们学习 Spring 时,我们了解到 Spring 最根本的使命就是 简化Java开发.而 SpringBoot 的出现也有 ...

  10. 解决扫码枪输入input时受中文输入法的影响

    <html><head> <meta content="text/html; charset=UTF-8" http-equiv="Cont ...