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只黑鼠 龙和王妃轮流从袋子里抓老鼠.谁先抓到白色老鼠谁就赢. 王妃每次 ...
随机推荐
- 九、chart控件的使用(图表数据的展示)
chart属于老插件网上还有新的插件功能也更多) Chart控件是VS自带的控件,只是比较老了,功能肯定不如现在出来的某些插件强,不过对于简单的显示还是够用:那么现在来看看chart控件的使用方法: ...
- ThinkPHP 入门
ThinkPHP是一个免费.开源的,快速.简单地面向对象的轻量级PHP开发框架,遵循Apache2开源协议发布,是为了敏捷Web应用开发和简化企业级应用开发而诞生的.ThinkPHP借鉴国外很多优秀的 ...
- C#编程之JSON序列化与反序列化
1.在C#管理NuGet程序包中添加Json.NET 2.C#将对象序列化成JSON字符串 模型类1 /// <summary> /// JSON字符串模型.是否出错 /// </s ...
- 使用腾讯云提供的针对Nuget包管理器的缓存加速服务
继阿里巴巴开源镜像站(https://opsx.alibaba.com/).华为云镜像站点(https://mirrors.huaweicloud.com/ )之后,腾讯也已于近日上线了类似的服务,官 ...
- 日常用shell命令
递归更改文件夹权限:chmod -R 767 文件名 mac启动apache sudo apachectl start/restart mac停止apache sudo apachectl stop ...
- 用python绘制漂亮的图形
先看效果,没有用任何绘图工具,只是运行了一段python代码. 代码如下: _ = ( 255, lambda V ,B,c :c and Y(V*V+B,B, c -1)if(abs(V)<6 ...
- Netty源码分析-- FastThreadLocal分析(十)
上节讲过了ThreadLocal的源码,这一节我们来看下FastThreadLocal.这个我觉得要比ThreadLocal要简单,因为缺少了对于Entry的清理和整理工作,所以ThreadLocal ...
- Git命令备忘录
目录 前言 基本内容 开始之前 基础内容 远程仓库 分支管理 前言 Git在平时的开发中经常使用,整理Git使用全面的梳理. 基本内容 开始之前 请自行准备好Git工具以及配置好Git的基本配置 基础 ...
- Spark 系列(七)—— 基于 ZooKeeper 搭建 Spark 高可用集群
一.集群规划 这里搭建一个 3 节点的 Spark 集群,其中三台主机上均部署 Worker 服务.同时为了保证高可用,除了在 hadoop001 上部署主 Master 服务外,还在 hadoop0 ...
- react学习(二)--元素渲染
元素用来描述你在屏幕上看到的内容: const element = <h1>Hello, world</h1>; 与浏览器的 DOM 元素不同,React 当中的元素事实上是普 ...