【题目链接】 http://acm.timus.ru/problem.aspx?space=1&num=1682

【题目大意】

  给出k,从1开始不断地加一并把这个数写在黑板上,如果写上的数字和之前的数字满足
  (a+b*b)%k=0或者(b+a*a)%k=0就在他们之间连一条线,如果黑板上出现环就结束,问能写几个数

【题解】

  我们发现写到2k-1的时候,就一定会产生一个环,所以我们只要枚举数字
  往满足要求的地方连边,判断是否出现环即可

【代码】

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std;
typedef long long LL;
const int N=300010;
int f[N],n,pre[N];
vector<int> G[N];
int sf(int x){return f[x]==x?x:f[x]=sf(f[x]);}
void solve(){
for(int i=1;i<=3*n;i++)pre[i]=0,f[i]=i,G[i].clear();
G[n-1].push_back(1);
for(LL i=2;i<=3*n;i++){
LL t=n-(i*i)%n;
while(t<i){
if(t){
if(sf(t)==sf(i)){printf("%lld\n",i);return;}
f[f[t]]=f[i]; pre[t]=i;
}t+=n;
}t=i%n;
for(int j=0;j<G[t].size();j++){
int p=G[t][j];
if(pre[p]==i)continue;
if(sf(p)==sf(i)){printf("%lld\n",i);return;}
f[f[p]]=f[i];
}G[n-(i*i)%n].push_back(i);
}
}
int main(){
while(~scanf("%d",&n))solve();
return 0;
}

URAL 1682 Crazy Professor (并查集)的更多相关文章

  1. 1682. Crazy Professor(并查集)

    1628 加了些数论知识  先看下剩余类的概念 一个整数被正整数n除后,余数有n种情形:0,1,2,3,…,n-1,它们彼此对模n不同余.这表明,每个整数恰与这n个整数中某一个对模n同余.这样一来,按 ...

  2. URAL 1671 Anansi's Cobweb (并查集)

    题意:给一个无向图.每次查询破坏一条边,每次输出查询后连通图的个数. 思路:并查集.逆向思维,删边变成加边. #include<cstdio> #include<cstring> ...

  3. URAL(timus)1709 Penguin-Avia(并查集)

    Penguin-Avia Time limit: 1.0 secondMemory limit: 64 MB The Penguin-Avia airline, along with other An ...

  4. URAL - 1003:Parity (带权并查集&2-sat)

    Now and then you play the following game with your friend. Your friend writes down a sequence consis ...

  5. ural1682 Crazy Professor

    Crazy Professor Time limit: 1.0 secondMemory limit: 64 MB Professor Nathan Mathan is crazy about mat ...

  6. *HDU1829 并查集

    A Bug's Life Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  7. POJ 2492 并查集扩展(判断同性恋问题)

    G - A Bug's Life Time Limit:10000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u S ...

  8. hdu 1829 A Bug's Life(分组并查集(偏移量))

    A Bug's Life Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  9. hdu 1829 基础并查集,查同性恋

    A Bug's Life Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) To ...

随机推荐

  1. 如何使用Navicat恢复数据库脚本

    Navicat 可以做数据库备份,当然也可以做数据库脚本恢复了.操作很简单. 1.连接需要恢复的数据库.鼠标右键点击,选择[运行SQL文件] 2.在弹出的窗口中选择sql文件,继续下一步即可. 余不赘 ...

  2. VK Cup 2016 - Round 1 (Div. 2 Edition) A Bear and Reverse Radewoosh

    A. Bear and Reverse Radewoosh time limit per test 2 seconds memory limit per test 256 megabytes inpu ...

  3. mybatis的mapper文件的大于号特殊符号使用

    第一种方法: 用了转义字符把>和<替换掉,然后就没有问题了. SELECT * FROM test WHERE 1 = 1 AND start_date  <= CURRENT_DA ...

  4. linux 下查看网卡工作速率

    [root@hadoop058 ~]# mii-tool eth0: negotiated 100baseTx-FD, link ok 100M linux 下查看网卡工作速率 Ethtool是用于查 ...

  5. Docker Community Edition for CentOS

    Docker CE for CentOS Docker CE for CentOS distribution is the best way to install the Docker platfor ...

  6. JavaScript 页面间传值

    转自:http://blog.csdn.net/qq380107165/article/details/7330612 一:JavaScript静态页面值传递之URL篇 能过URL进行传值,把要传递的 ...

  7. 一个IT中专生在深圳的9年辛酸经历

    一个IT中专生在深圳的9年辛酸经历 想一想来到深圳已经近10年了,感概万千呐!从一个身无分文的中专职校计算机毕业出来后,竟然大胆的南下(之前可是连我们那地区之外都没去过),现在有供完的房子,温柔的妻子 ...

  8. PHP等比例生成缩略图

    /** * 生成缩略图 * $imgSrc 图片源路径 * $resize_width 图片宽度 * $resize_height 图片高度 * $dstimg 缩略图路径 * $isCut 是否剪切 ...

  9. 知问前端——日历UI(三)

    datepicker日期选择选项 属性 默认值/类型 说明 minDate 无/对象.字符串或数值 日历中可以选择的最小日期 maxDate 无/对象.字符串或数值 日历中可以选择的最大日期 defa ...

  10. PHP文件操作函数二

    PHP部分文件访问函数总结: 1.filetype("文件路径")  //可以输出相关文件类型,返回之为:dir/file... 2.stat("文件名") / ...