题目链接: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. homestead修改php版本

    登录后 如果之前没有设置过root密码 sudo passwd root 以root 权限执行如下命令,选择对应php版本 # 查看所有 php 版本和当前版本 update-alternatives ...

  2. JetBrains视图

    三种视图模式:

  3. 卸载yum-mysql

    注意事项:1. 卸载yum MYSQLsystemctl status mysqlsystemctl stop mysqlsystemctl disable mysqld rpm -qa | grep ...

  4. 四、JVM — 类文件结构

    类文件结构 一 概述 二 Class 文件结构总结 2.1 魔数 2.2 Class 文件版本 2.3 常量池 2.4 访问标志 2.5 当前类索引,父类索引与接口索引集合 2.6 字段表集合 2.7 ...

  5. ajax后台请求两种方法(js和jQuery)

    (1)js的ajax var xmlHttp; if(window.XMLHttpRequest){ xmlHttp=new XMLHttpRequest(); }else{ xmlHttp=new ...

  6. Fusioncharts图表常用参数设置

    1.1 <chart>参数设置: 图表和轴的标题* caption=”String” : 图表上方的标题* subCaption=”String” : 图表上方的副标题* xAxisNam ...

  7. XMPP实现原理

    XMPP协议简介   XMPP(Extensible Messageing and Presence Protocol:可扩展消息与存在协议)是目前主流的四种IM(IM:instant messagi ...

  8. 2019-10-31-VisualStudio-2019-新特性

    title author date CreateTime categories VisualStudio 2019 新特性 lindexi 2019-10-31 08:48:27 +0800 2019 ...

  9. 关于jsp 获得当前绝对路径的方法

    方法1) request.getRequestURL(); 方法2)  request.getScheme()+"://"+request.getServerName()+&quo ...

  10. MFC学习笔记3---使对话框风格与系统统一

    有一件郁闷了我很久的事情,在VS中编辑对话框或者点击预览时都是以Win7风格体现的按钮及对话框: 点击上图测试对话框: 然而生成的应用程序却是这样的: 这样人很不爽啊,按钮风格回到了N年前的版本,复古 ...