题意略。

思路:

如果是问一下然后搜一下,那必然是不现实的。因此我们要预处理出所有的答案。

我们令mod = lcm(m1,m2,...,mn)。可知,在任意一点,我们挑选两个不同的数c1、c2,其中c2 = k * mod + c1,这两种出发状态一定会走出相同的路径。

由此,我们把每个点拆成mod个状态点,那一共是n * mod个点,由每个状态点只引申出来一条只想别的点的边,我们其实就是要在这个有向图中找环,

找环后统计环上不同点的个数。

开始的时候,我以为环的个数不会超过实际点的个数,后来wa了一次,发现同一个实际点其实是可以在不同的环中的。

代码如下:

#include<bits/stdc++.h>
using namespace std;
const int maxn = ;
const int maxm = ;
const int maxp = ; int connect[maxn],belong[maxn],scc[maxn],cnt;
int stk[maxn],tail;
bool visit[maxn]; int n,mstore[maxp],kstore[maxp],mod = ;
vector<int> graph[maxp];
set<int> st,sst; void add_e(int u,int v){
connect[u] = v;
}
int gcd(int a,int b){
return b == ? a : gcd(b,a % b);
}
int lcm(int a,int b){
int d = gcd(a,b);
return a / d * b;
}
void dfs(int p){
if(visit[p]) return;
st.clear();
sst.clear();
tail = ;
while(!visit[p]){
visit[p] = true;
stk[tail++] = p;
st.insert(p);
p = connect[p];
}
if(st.count(p)){
int idx = cnt++;
while(stk[tail - ] != p){
int cur = stk[--tail];
belong[cur] = idx;
cur = cur / mod + ;
sst.insert(cur);
}
--tail;
sst.insert(p / mod + );
belong[p] = idx;
scc[idx] = sst.size();
}
for(int i = ;i < tail;++i){
belong[stk[i]] = belong[p];
} } int main(){
scanf("%d",&n);
for(int i = ;i <= n;++i) scanf("%d",&kstore[i]);
for(int i = ;i <= n;++i){
scanf("%d",&mstore[i]);
int m = mstore[i],temp;
mod = lcm(mod,m);
for(int j = ;j < m;++j){
scanf("%d",&temp);
graph[i].push_back(temp);
}
}
for(int i = ;i <= n;++i){
int m = mstore[i];
for(int j = ;j < mod;++j){
int to = graph[i][j % m];
int keep = to;
to = j + kstore[to];
to = (to % mod + mod) % mod;
to = (keep - ) * mod + to;
int from = (i - ) * mod + j;
add_e(from,to);
}
}
int tot = n * mod;
for(int i = ;i < tot;++i) dfs(i);
int x,y,q;
scanf("%d",&q);
for(int i = ;i < q;++i){
scanf("%d%d",&x,&y);
y = (y + kstore[x]) % mod;
y = (y + mod) % mod;
int cur = (x - ) * mod + y;
int fa = belong[cur];
int ans = scc[fa];
printf("%d\n",ans);
}
return ;
}

CodeForces 1200F的更多相关文章

  1. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  2. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  3. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

  4. 【Codeforces 738A】Interview with Oleg

    http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...

  5. CodeForces - 662A Gambling Nim

    http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...

  6. CodeForces - 274B Zero Tree

    http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...

  7. CodeForces - 261B Maxim and Restaurant

    http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...

  8. CodeForces - 696B Puzzles

    http://codeforces.com/problemset/problem/696/B 题目大意: 这是一颗有n个点的树,你从根开始游走,每当你第一次到达一个点时,把这个点的权记为(你已经到过不 ...

  9. CodeForces - 148D Bag of mice

    http://codeforces.com/problemset/problem/148/D 题目大意: 原来袋子里有w只白鼠和b只黑鼠 龙和王妃轮流从袋子里抓老鼠.谁先抓到白色老鼠谁就赢. 王妃每次 ...

随机推荐

  1. FAIRR

    FAIRR 在进行一项工作时需要注意学习.应用和改进已有信息和成果,可参考FAIRR原则: Find existing info and result, Add to and Improve it, ...

  2. TP框架基础(四)----添加数据

    [数据添加] add() 该方法返回被添加的新记录的主键id值 两种方式实现数据添加 1. 数组方式数据添加 $goods = D(“Goods”); $arr = array(‘goods_name ...

  3. Docker volume speed up npm install

    上一节决定在Jenkins中采用Docker作为构建环境,于是就可以为所欲为的使用各种node版本编译我们的项目.解决了版本切换问题.然而,Docker设计的目的就是纯净的执行环境,因此每次运行doc ...

  4. Integrating Thymeleaf with Spring

    这个是基于注解的配置方式,基于配置文件的http://www.cnblogs.com/honger/p/6875148.html 一.整体结构图 二.web.xml文件,这里使用了注解的方式 < ...

  5. 图解Redis之数据结构篇——整数集合

    前言     整数集合(intset)并不是一个基础的数据结构,而是Redis自己设计的一种存储结构,是集合键的底层实现之一,当一个集合只包含整数值元素,并且这个集合的元素数量不多时, Redis i ...

  6. wangEditor富文本编辑器使用及图片上传

    引入js文件 <script type="text/javascript" src="style/js/wangEditor.min.js">< ...

  7. superset安装文档

    1 安装python3.6 yum install epel-release -y yum install https://centos7.iuscommunity.org/ius-release.r ...

  8. git指令-未完待更新

    git指令 1. $ git config --global user.name "Your Name" $ git config --global user.email &quo ...

  9. win7 python pdf2image入坑经历

    Python开发菜鸟入坑 项目要求pdf转成图片,网上较多的方案对于windows极其不友好,wand,Pythonmagick(win下载地址:www.lfd.uci.edu/~gohlke/pyt ...

  10. Linux 根分区扩容

    扩容分区之前,首先要保证当前有闲置空间 1. 查看当前现有分区情况 df -lah 可以看出当前根分区只剩 6.4 G 可用 2. 查看当前磁盘情况 fdisk -l 可以看出有 30G的未分配空间 ...