CodeForces 1200F
题意略。
思路:
如果是问一下然后搜一下,那必然是不现实的。因此我们要预处理出所有的答案。
我们令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的更多相关文章
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
- 【Codeforces 738A】Interview with Oleg
http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...
- CodeForces - 662A Gambling Nim
http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...
- CodeForces - 274B Zero Tree
http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...
- CodeForces - 261B Maxim and Restaurant
http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...
- CodeForces - 696B Puzzles
http://codeforces.com/problemset/problem/696/B 题目大意: 这是一颗有n个点的树,你从根开始游走,每当你第一次到达一个点时,把这个点的权记为(你已经到过不 ...
- CodeForces - 148D Bag of mice
http://codeforces.com/problemset/problem/148/D 题目大意: 原来袋子里有w只白鼠和b只黑鼠 龙和王妃轮流从袋子里抓老鼠.谁先抓到白色老鼠谁就赢. 王妃每次 ...
随机推荐
- SQLyog12最新版破解
1.SQLyog-12.2.4-0.x64Trial.exe,直接去官网下载. 2.修改注册表项 开始-运行-regedit ,进入注册表 HKEY_CURRENT_USER\Software\ ...
- MetInfo企业网站管理系统 5.3 全新安装
在phpStudy\PHPTutorial\WWW的文件下创建MetInfo文件夹.把MetInfo5.3解压到MetInfo文件夹里 用浏览器访问127.0.0.1/MetInfo 同意安装 下一步 ...
- .NET Core 3.0之深入源码理解HttpClientFactory(二)
写在前面 上一篇文章讨论了通过在ConfigureServices中调用services.AddHttpClient()方法,并基于此进一步探讨了DefaultHttpClientFactory是 ...
- 【Android Studio】类名字右下角红色的 J 【待解决】
问题如下图所示: 正在寻找结解决方法--
- 【iOS】判断苹果的设备是哪种
有时候需要判断苹果的设备是 iPhone 还是 iPad 等其他设备,示例代码如下: if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUs ...
- Svn提交冲突问题
MEclipse中的svn冲突解决办法: 1. 点击提交,报错——‘SVN提交’has encountered a problem. 2. 选中无法提交的文件,点击更新操作 ...
- 第四次作业;创建raid5,源码编译安装;磁盘配额
创建raid5 格式化 ext4 创建物理卷: 创建卷组: 创建逻辑卷: 格式化 ext4 挂载 开机自启动 创建raid配置文件 源码编译安装: 创建本地yum仓库 umount /dev/sr0 ...
- 洛谷P2763题解
吐槽一下:蜜汁UKE是什么玩意?! 题目分析: 观察题面,对于给定的组卷要求,计算满足要求的组卷方案,可以发现这是一道明显的有条件的二分图匹配问题,于是考虑建模. 建一个超级源点,一个超级汇点:源点与 ...
- 微信公众号接入服务器验证(Go实现)
1 基本流程 将token.timestamp.nonce三个参数进行字典序排序 将三个参数字符串拼接成一个字符串进行sha1加密 开发者获得加密后的字符串可与signature对比,标识该请求来源于 ...
- 【Java笔记】【Java核心技术卷1】chapter3 D2注释
package chapter3; /** * 文档注释 *@author lp *@version 1 **/ public class D2注释 { //单行注释 /* 长注释 */ }