URAL 1682 Crazy Professor (并查集)

【题目链接】 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;
}
时间: 2024-12-21 05:21:58

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

URAL 1671 Anansi&#39;s Cobweb (并查集)

题意:给一个无向图.每次查询破坏一条边,每次输出查询后连通图的个数. 思路:并查集.逆向思维,删边变成加边. #include<cstdio> #include<cstring> #include<algorithm> #include<vector> #include<iostream> #define inf -100000000 #define LL long long #define maxn 100005 using namespace

Pilot Work Experience (URAL 1888 并查集+floyd)

Pilot Work Experience Time Limit: 1000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u Submit Status Description Leonid had n Oceanic Airlines flights during his business trip. He studied the latest issue of the monthly on-board magazine o

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

Penguin-Avia Time limit: 1.0 secondMemory limit: 64 MB The Penguin-Avia airline, along with other Antarctic airlines, experiences financial difficulties because of the world's economic crisis. People of Antarctica economize on flights and use trains

Crazy Professor

Crazy Professor Time limit: 1.0 secondMemory limit: 64 MB Professor Nathan Mathan is crazy about mathematics. For an unknown reason, he started to write on the blackboard all positive integers starting from 1. After writing a new number a, Professor

*HDU1829 并查集

A Bug's Life Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 14121    Accepted Submission(s): 4603 Problem Description Background Professor Hopper is researching the sexual behavior of a rare s

poj2492--A Bug&#39;s Life(并查集变形)

A Bug's Life Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 28703   Accepted: 9350 Description Background Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes that they feature two different genders

POJ2492 A Bug&#39;s Life 【并查集】

A Bug's Life Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 29011   Accepted: 9451 Description Background Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes that they feature two different genders

hdu 1829 A Bug&#39;s Life(分组并查集(偏移量))

A Bug's Life Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 9204    Accepted Submission(s): 2961 Problem Description Background Professor Hopper is researching the sexual behavior of a rare sp

hdoj-1289-A Bug&amp;#39;s Life【种类并查集】

A Bug's Life Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 11386 Accepted Submission(s): 3709 Problem Description Background Professor Hopper is researching the sexual behavior of a rare specie