题意略。

思路:

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

我们令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. C#中使用split分割字符串的方法小结

    string s=abcdeabcdeabcde; string[] sArray=s.Split(c) ; foreach(string i in sArray) Console.WriteLine ...

  2. Apache Tomcat 绿色版安装Service(服务)

    1.配置CATALINA_HOME的环境变量:  变量名:CATALINA_HOME  值:tomcat安装或解压的根目录如:c:\Apache tomcat6.0 2.开始->运行->c ...

  3. 说说Java线程间通信

    序言 正文 [一] Java线程间如何通信? 线程间通信的目标是使线程间能够互相发送信号,包括如下几种方式: 1.通过共享对象通信 线程间发送信号的一个简单方式是在共享对象的变量里设置信号值:线程A在 ...

  4. [POJ2823] Sliding Window 「单调队列」

    我们从最简单的问题开始: 给定一个长度为N的整数数列a(i),i=0,1,...,N-1和窗长度k. 要求:   f(i) = max{ a(i-k+1),a(i-k+2),..., a(i) },i ...

  5. (11)ASP.NET Core 中的配置一(Configuration)

    1.前言 ASP.NET Core在应用程序上引入Microsoft.Extensions.Configuration配置,可以支持多种方式配置,包括命令行配置.环境变量配置.文件配置.内存配置,自定 ...

  6. iOS开发 8小时时差问题

    今天调试遇到时间计算的问题,发现怎么算都会有差别,后来仔细观察,发现有8小时的时差…… 这篇文章解释的很好,用到了,因此记之. ios有关时间打印出来差8小时的问题

  7. ld: library not found for -

    这几天在做微信登录,总是遇到这个问题,详细如下: ld: library not found for -lWeChatSDK clang: error: linker command failed w ...

  8. Redis 学习笔记(篇七):Redis 持久化

    因为 Redis 是内存数据库,它将自己的数据储存在内存里面,所以如果不想办法将储存在内存中的数据库状态保存到磁盘里面,那么一旦服务器进程退出,服务器中的数据也将会丢失,为了解决这个问题,Redis ...

  9. Apple放大绝进行反取证

    取证说穿了其实就是攻防,这本是正义与邪恶的对决,亦即执法单位与嫌疑犯两者之间的事,但现实生活中要比这复杂多了. 怎么说呢?举个例子大家便理解了.取证人员费尽心思,用尽各种手法,努力地想要自手机上提取重 ...

  10. dotnetcore 与 hbase 之二——thrift 客户端的制作

    说明 在上一篇文章dotnetcore 与 hbase 之一--hbase 环境准备结束后,我们已经有了 hbase 数据库环境.接下来就可以利用 thrift 生成 c# hbase 客户端了.如果 ...