题目链接: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. C++中的const分析

    1,C 语言中的 const: 1,const 修饰的变量是只读的,本质还是变量: 1,C 语言中的 const 使变量具有只读属性: 2,const 只在编译期有用,在运行期无用: 3,const ...

  2. 洛谷P3366【模板】最小生成树-克鲁斯卡尔Kruskal算法详解附赠习题

    链接 题目描述 如题,给出一个无向图,求出最小生成树,如果该图不连通,则输出orz 输入输出格式 输入格式: 第一行包含两个整数N.M,表示该图共有N个结点和M条无向边.(N<=5000,M&l ...

  3. Python和Js打印心形

    看到一行Python写的代码,会用LovePython输出心形: print('\n'.join([''.join([('LovePython'[(x-y)%10]if((x*0.05)**2+(y* ...

  4. MinGW的安装

    我在MinGW官网下载到的版本是mingw-w64-install.exe,不过这差不多是一年以前的事了……   安装路径:D:\Program Files (x86)\mingw-w64\i686- ...

  5. django部署到linux上不显示.svg图标处理方法

    在setting文件的最开始添加如下内容: import mimetypes mimetypes.add_type("image/svg+xml", ".svg" ...

  6. 7.css3表格、列表、边框的样式设置--list/border

    1.css表格: ①Border-collapse是否把表格边框合并为单一的边框.Separate默认值,collapse合并. ②Border-spacing分割单元格边框的距离. ③Caption ...

  7. HTML回顾之表格

    HTML表格 由什么组成? 表格由<table>标签来定义.每个表格有若干行(<tr>标签来定义),每行被分割成若干单元格(<td>标签来定义). td值表格数据, ...

  8. Vue Google浏览器插件 Vue Devtools无法使用的解决办法

    1.插件安装不必多说  一定要用Vue.js 开发版    Vue.min.js 在控制面板就不会显示 2.本地调试 用的是file://协议 修改插件允许访问文件网址  打上对勾

  9. PAT Advanced 1009 Product of Polynomials (25 分)(vector删除元素用的是erase)

    This time, you are supposed to find A×B where A and B are two polynomials. Input Specification: Each ...

  10. shell中通过读取输入yes no判断下一步如何处理

    if [ -d $r_item_rmgit ];then                            read -p "$r_item_rmgit exit, replace it ...