Do you think that magic is simple? That some hand-waving and muttering incomprehensible blubber is enough to conjure wonderful gardens or a fireball to burn your enemies to ashes?
The reality is a little more complicated than that. To master skills, young wizards spend years studying such subjects as magical analysis and demonology practice.
In fact, Oleg, a student of the Institute of Magic and Common Sorcery (IMCS) is preparing for an exam. And there’s no way he can calculate the Dumbledore determinant. As you might have guessed, he asked you to help him.
Let us remind you the basic definitions just in case you haven’t been visiting lectures on the theory of nonlinear spells. The Gandalf theorem states that any part of subspace can be represented as a vector of magic potentials that is an array of n positive integers. A Dumbledore determinant of this array equals the minimum number of elementary magical transformations required to turn the original array into the array where all elements are equal to one. One elementary magical transformation turns the original array of length k into a new array of length k · (k − 1) / 2. The elements of the new array are greatest common divisors of each pair of elements of the original array. For example, the elementary magical transformation of array {2, 3, 3, 6} turns it into array {gcd(2, 3), gcd(2, 3), gcd(2, 6), gcd(3, 3), gcd(3, 6), gcd(3, 6)}, that is {1, 1, 2, 3, 3, 3}.

Input

The first line contains number n that is the length of the original array (3 ≤ n ≤ 10 000). Next n lines contain the elements of array that are positive integers not exceeding 107.

Output

Output Dumbledore determinant for the array given in the input. If Dumbledore determinant is not defined or it exceeds 1018, output “infinity”.

Samples

input output
3
1
2
3
1
4
2
2
2
2
infinity
Problem Author: Kirill Borozdin

题意:给定N个数,每一轮变换成两两对应的GCD:即变换前是X个数,变换后是X*(X-1)/2个数。如: {2, 3, 3, 6} turns it into array {gcd(2, 3), gcd(2, 3), gcd(2, 6), gcd(3, 3), gcd(3, 6), gcd(3, 6)}, that is {1, 1, 2, 3, 3, 3}.                问第几次变换后全部变为1,如果不行,输出“infinity”。

思路:如果有某一轮变换前有三个或以上的相同的数(不等于1),则不可能全部变为1,如样例的2,2,2。但是不可能模拟每一轮转化的过程。我们换个角度:如果一个因子在大于等于三个数里出现,则者三个数会相互影响,一直繁殖下去.所以答案为:

0:已经全部是1

1:所有数互质

2:有相同因子,但是有同一因子的个数不大于2.

inf:存在一个因子,在操作两个数里出现过。

(坚持A掉题之前不看题解,自己做!!!加油)

#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=;
int p[maxn+],vis[maxn+],num[maxn*+],cnt;
void getprime()
{
for(int i=;i<=maxn;i++){
if(!vis[i]) p[++cnt]=i;
for(int j=;j<=cnt&&i*p[j]<=maxn;j++){
vis[i*p[j]]=;
if(i%p[j]==) break;
}
}
}
int main()
{
getprime();
int N,i,j,x;
bool F=true;
scanf("%d",&N);
for(i=;i<=N;i++){
scanf("%d",&x);
if(x!=) F=false;
for(j=;j<=cnt;j++){
if(x%p[j]==){
num[p[j]]++;
while(x%p[j]==) x/=p[j];
}
}
if(x>) num[x]++;
}
if(F) {
printf("0\n");
return ;
}
for(i=;i<=;i++)
if(num[i]>){
printf("infinity\n");
return ;
}
for(i=;i<=;i++)
if(num[i]==){
printf("2\n");
return ;
}
printf("1\n");
return ;
}

Ural 2003: Simple Magic(数论&思维)的更多相关文章

  1. ural 2066. Simple Expression

    2066. Simple Expression Time limit: 1.0 secondMemory limit: 64 MB You probably know that Alex is a v ...

  2. Maximal GCD CodeForces - 803C (数论+思维优化)

    C. Maximal GCD time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  3. CF 1064B Equations of Mathematical Magic(思维规律)

    Description Colossal! — exclaimed Hawk-nose. — A programmer! That's exactly what we are looking for. ...

  4. [Hdu-5155] Harry And Magic Box[思维题+容斥,计数Dp]

    Online Judge:Hdu5155 Label:思维题+容斥,计数Dp 题面: 题目描述 给定一个大小为\(N*M\)的神奇盒子,里面每行每列都至少有一个钻石,问可行的排列方案数.由于答案较大, ...

  5. 2019牛客多校第三场H Magic Line 思维

    Magic Line 题意 给出n(偶)个整点 整点范围1000,找出一条直线,把n个点分成均等的两部分 分析 因为都是整数,并且范围比较小,所以直接按x排序找到在中间那一部分,并且把中间那一部分的点 ...

  6. URAL 2066 Simple Expression (水题,暴力)

    题意:给定三个数,让你放上+-*三种符号,使得他们的值最小. 析:没什么好说的,全算一下就好.肯定用不到加,因为是非负数. 代码如下: #pragma comment(linker, "/S ...

  7. zoj Simple Equation 数论

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5176 AX+BY = XY  => (X-B)*(Y-A)= ...

  8. URAL 1133 Fibonacci Sequence(数论)

    题目链接 题意 :给你第 i 项的值fi,第 j 项的值是 fj 让你求第n项的值,这个数列满足斐波那契的性质,每一项的值是前两项的值得和. 思路 :知道了第 i 项第j项,而且还知道了每个数的范围, ...

  9. URAL - 2065 Different Sums (思维题)

    题意: 给n和k,让你用不小于 k 个不同的数字构成一个长度为n的序列,使得序列中不同的区间和的数目最小. n,k<=500 k-1个数填一些数字的一正一负,这样有些区间和为0. 剩下的都填0. ...

随机推荐

  1. Python 单向队列Queue模块详解

    Python 单向队列Queue模块详解 单向队列Queue,先进先出 '''A multi-producer, multi-consumer queue.''' try: import thread ...

  2. python022 Python3 面向对象

    Python3 面向对象 Python从设计之初就已经是一门面向对象的语言,正因为如此,在Python中创建一个类和对象是很容易的.本章节我们将详细介绍Python的面向对象编程. 如果你以前没有接触 ...

  3. 搭建双塔(vijos 1037)

    描述 2001年9月11日,一场突发的灾难将纽约世界贸易中心大厦夷为平地,Mr. F曾亲眼目睹了这次灾难.为了纪念“9?11”事件,Mr. F决定自己用水晶来搭建一座双塔. Mr. F有N块水晶,每块 ...

  4. Codeforces Round #295 D. Cubes [贪心 set map]

    传送门 D. Cubes time limit per test 3 seconds memory limit per test 256 megabytes input standard input ...

  5. msp430项目编程11

    msp430中项目---步进电机控制系统 1.步进电机工作原理 2.电路原理说明 3.代码(显示部分) 4.代码(功能实现) 5.项目总结 msp430项目编程 msp430入门学习

  6. Delphi控件大全

    首先来大体上为控件分一下类,以方便我们后面的讨论.   但因为控件的种类太多,所以就粗略的分为如下几个类别∶   ---界面风格类   ---Shell外观类   ---Editor类   ---Gr ...

  7. HDU 3966

    树链剖分 练模板: 用的 是HH的线段树 虽然之前是我不用的摸板 修改区间 求点值: CODE: #pragma comment(linker,"/STACK:1024000000,1024 ...

  8. MySQL中的数据类型的长度范围和显示宽度(转)

    长度范围是随数据类型就已经是固定的值,而显示宽度与长度范围无关. 以下是每个整数类型的存储和范围(来自MySQL手册) 类型 字节 最小值 最大值 (带符号的/无符号的) (带符号的/无符号的) TI ...

  9. Atom替换换行符

    直接[Ctrl]+[F],然后选择正则,输入\n

  10. Meteor ToDo App实例

    在本章中,我们将创建一个简单的待办事项应用程序. 第1步 - 创建应用程序 打开命令提示符,运行以下命令 - C:\Users\Administrator\Desktop>meteor crea ...