Crazy Professor

Time limit: 1.0 second
Memory 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 draws lines connecting it with all the numbers b that are already on the blackboard and satisfy at least one of the conditions:

  • b + a · a ≡ 0 (mod k),
  • a + b · b ≡ 0 (mod k),

where k is some parameter.

Nobody can persuade him to stop this meaningless procedure. Professor says that he will stop as soon as there appears a cycle in the graph of the numbers on the blackboard. But only Professor knows when that will happen and whether it will happen at all. Help his colleagues determine after which number he will stop.

Input

You are given the integer k (1 ≤ k ≤ 100000).

Output

Output the number after which the first cycle will appear in the graph. If it never happens, output −1.

Sample

input output
2
5

Notes

In example after Professor had written all integers from 1 to 4 the graph contained edges (1, 3) and (2, 4). After writing number 5, Professor connects it with numbers 1 and 3, so the cycle 1-5-3-1 appears in the graph.
分析:并查集+同余定理;
   注意可能有两条重边,要排除;
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, rt<<1
#define Rson mid+1, R, rt<<1|1
const int maxn=2e5+;
using namespace std;
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
int n,m,k,t,p[maxn],now[maxn];
vi mo[maxn];
int find(int x)
{
return p[x]==x?x:p[x]=find(p[x]);
}
int main()
{
int i,j;
scanf("%d",&k);
rep(i,,maxn-)p[i]=i;
for(i=;;i++)
{
ll q=((ll)i*i/k+)*k;
while((j=q-(ll)i*i)<i)
{
int x=find(j),y=find(i);
if(x==y)return *printf("%d\n",i);
else p[x]=y;
now[j]=i;
q+=k;
}
q=(ll)(i/k+)*k;
for(int x:mo[(q-i)%k])
{
if(now[x]==i)continue;
int y=find(x),r=find(i);
if(y==r)return *printf("%d\n",i);
else p[y]=r;
}
mo[(ll)i*i%k].pb(i);
}
//system("Pause");
return ;
}

ural1682 Crazy Professor的更多相关文章

  1. 1682. Crazy Professor(并查集)

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

  2. URAL 1682 Crazy Professor (并查集)

    [题目链接] http://acm.timus.ru/problem.aspx?space=1&num=1682 [题目大意] 给出k,从1开始不断地加一并把这个数写在黑板上,如果写上的数字和 ...

  3. DevOps is dirty work - CI drives you crazy

    一直很想谈谈Continuous Integration(CI),持续集成. 就在不久前一次朋友聚会上,一个刚刚跳槽到一家创业公司的朋友跟我抱怨说他们没有CI,没有code review,要做点事太累 ...

  4. Here's to the crazy ones.

    Here's to the crazy ones. The misfits. The rebels. The troublemakers. The round pegs in the square h ...

  5. Urban Planning and Public Health - Reflection on Professor Webster's article in Urban Planning Forum

    1. General review. Professor Webster published this article in Urban Planning Forum, one of the top ...

  6. [poj3378] Crazy Thairs (DP + 树状数组维护 + 高精度)

    树状数组维护DP + 高精度 Description These days, Sempr is crazed on one problem named Crazy Thair. Given N (1 ...

  7. [poj1200]Crazy Search(hash)

    Crazy Search Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 26713 Accepted: 7449 Descrip ...

  8. Light OJ 1393 Crazy Calendar (尼姆博弈)

    C - Crazy Calendar Time Limit:4000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Su ...

  9. THE ARCHITECTURE OF COMPLEXITY HERBERT A. SIMON* Professor of Administration, Carnegie Institute of Technology (Read April 26, 1962)

    THE ARCHITECTURE OF COMPLEXITY HERBERT A. SIMON* Professor of Administration, Carnegie Institute of ...

随机推荐

  1. Java多线程之~~~使用Exchanger在线程之间交换数据[这个结合多线程并行会有解决很多问题]生产者消费者模型

    http://blog.csdn.net/a352193394/article/details/39503857  Java多线程之~~~使用Exchanger在线程之间交换数据[这个结合多线程并行会 ...

  2. Python识别网站验证码

    http://drops.wooyun.org/tips/6313 Python识别网站验证码 Manning · 2015/05/28 10:57 0x00 识别涉及技术 验证码识别涉及很多方面的内 ...

  3. nodejs全局安装与本地安装区别

    本地安装 1. 将安装包放在 ./node_modules 下(运行 npm 命令时所在的目录),如果没有 node_modules 目录,会在当前执行 npm 命令的目录下生成 node_modul ...

  4. Codeforce#354_B_Pyramid of Glasses(模拟)

    题目连接:http://codeforces.com/contest/676/problem/B 题意:给你一个N层的杯子堆成的金字塔,倒k个杯子的酒,问倒完后有多少个杯子的酒是满的 题解:由于数据不 ...

  5. csu1010: Water Drinking

    /* 本题的题意: 沙漠中有很多骆驼和一个池塘,0表示池塘,1-N表示骆驼,输入的两个数表示两只骆驼,其中前面的那一头靠近池塘,所有的骆驼队列不交叉不相连,求站在队尾但是离水井最近的骆驼编号 经过分析 ...

  6. js调用函数的格式

    如题 onclick='alert(\""+"&#1"+"\")' onclick='alert(encodeURIComponen ...

  7. Docker 简介及安装

    Docker简介: 什么是Docker?将应用程序自动部署到容器 go语言开源引擎  Github地址:https://github.com/docker/docker 2013年初 dotCloud ...

  8. VB webbrowser 控件的应用(跨域 内嵌网页元素的访问)

    自动登录财付通,难点在于会出现验证码,并且验证码页是在iframe元素下的,出于各种安全考虑,webbrowser控件不提供这种访问机制!当然,第一想到的是将这个网页拿出来,可是输入完毕验证码后,点击 ...

  9. 虚拟机centos分区

    在计算机上安装 Linux 系统,对硬盘进行分区是一个非常重要的步骤,下面介绍几个分区方案. 方案 1 / :建议大小在 5GB 以上. swap :即交换分区,建议大小是物理内存的 1~2 倍. 方 ...

  10. OpenGL ES之glUniform函数

    函数名: glUniform 功能: 为当前程序对象指定Uniform变量的值.(译者注:注意,由于OpenGL ES由C语言编写,但是C语言不支持函数的重载,所以会有很多名字相同后缀不同的函数版本存 ...