题目链接: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. [BZOJ 3930] [CQOI 2015]选数(莫比乌斯反演+杜教筛)

    [BZOJ 3930] [CQOI 2015]选数(莫比乌斯反演+杜教筛) 题面 我们知道,从区间\([L,R]\)(L和R为整数)中选取N个整数,总共有\((R-L+1)^N\)种方案.求最大公约数 ...

  2. tensorflow学习笔记二----------变量

    tensorflow里面的变量表示,需要使用特定的语法进行.如果想构造一个行(列)向量,需要调用Variable函数进行.对两个变量进行操作,也要调用相应的函数. import tensorflow ...

  3. Python中对 文件 的各种骚操作

    Python中对 文件 的各种骚操作 python中对文件.文件夹(文件操作函数)的操作需要涉及到os模块和shutil模块. 得到当前工作目录,即当前Python脚本工作的目录路径: os.getc ...

  4. Docker:删除images报错(Error response from daemon: conflict: unable to delete 6885a5e3712a (must be forced) - image is being used by stopped container 0cf27d7d29c7)

    1.进入root权限 sudo su 2.停止所有的container,这样才能够删除其中的images: docker stop $(docker ps -a -q) 如果想要删除所有contain ...

  5. wordpress中page页添加非插件留言板功能

    把下面的代码插入到page页面中即可 <!-- 留言板 --> <div class="wrap"> <div id="primary&qu ...

  6. Git 使用疑问

    1)  fatal: remote origin already exists. 解决办法 ..$ git remote add origin git@git.*.com:tang/comment_s ...

  7. Elastic Search闪退问题

    昨天还可以正常启动,今天及不行.. 在网上找了很多方法都不行,后来参考https://blog.csdn.net/happyzxs/article/details/89156068,修复好了 一.遇到 ...

  8. ES6初步学习

    from:http://www.jianshu.com/p/287e0bb867ae 刚开始用vue或者react,很多时候我们都会把ES6这个大兄弟加入我们的技术栈中.但是ES6那么多那么多特性,我 ...

  9. Qt是什么?Qt简介(非常全面)

    http://c.biancheng.net/view/1792.html Qt(官方发音 [kju:t],音同 cute)是一个跨平台的 C++ 开发库,主要用来开发图形用户界面(Graphical ...

  10. Thinkphp5 手册

    thinkphp5 手册 https://www.kancloud.cn/manual/thinkphp5/118003