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. C#排序1(冒泡排序、直接排序、快速排序)

    冒泡排序:就是两个两个的这个比较好理解,代码也比较好写出来. 它的原理就是相邻的两个两个的比较,如果前面的数比后面的大,那么交换,它这个在比较完一次的时候可以得到最大的一个数,然后接着循环,每次外循环 ...

  2. centos 6.5 yum安装lnmp

    转自:http://blog.csdn.net/lane_l/article/details/20235909 准备篇: 端口 vi /etc/sysconfig/iptables -A INPUT ...

  3. CDQ分治模板

    #include<cstdio> #include<algorithm> #include<cstring> #include<cmath> #defi ...

  4. java容器详解(以Array Arrays ArrayList为例)

    //先记录一个想法……java实在是太臃肿了,纯面向对象也有不少弊端…… //能不能把java精简一下啊! 先上结论: Array:认真看api索引的话,Array有两个.一个是sql中的接口,一个是 ...

  5. 2018 百度之星 初赛 第六题 HDU6349

    三原色图  Accepts: 281  Submissions: 1261  Time Limit: 1500/1000 MS (Java/Others)  Memory Limit: 262144/ ...

  6. 洛谷—— P1977 出租车拼车

    https://www.luogu.org/problem/show?pid=1977 题目背景 话说小 x 有一次去参加比赛,虽然学校离比赛地点不太远,但小 x 还是想坐 出租车去.大学城的出租车总 ...

  7. T2627 村村通 codevs

    http://codevs.cn/problem/2627/  时间限制: 1 s  空间限制: 32000 KB  题目等级 : 黄金 Gold 题目描述 Description 农民约翰被选为他们 ...

  8. noip 2015 day1

    T1 神奇的幻方 题目描述 幻方是一种很神奇的N*N矩阵:它由数字1,2,3,……,N*N构成,且每行.每列及两条对角线上的数字之和都相同. 当N为奇数时,我们可以通过以下方法构建一个幻方: 首先将1 ...

  9. List和Map、Set的区别

    首先 List 和 Set 是存储单列数据的集合,Map 是存储键和值这样的双列数据的集合:List 中存储的数据是有顺序,并且允许重复:Map 中存储的数据是没有顺序的,其键是不能重复的,它的值是可 ...

  10. java 并发基础,及案例分析

    对于我们开发的网站,如果网站的访问量非常大的话,那么我们就需要考虑相关的并发访问问题了,然而并发问题是令我们大多数程序员头疼的问题,但话又说回来了,既然逃避不掉,那我们就坦然面对吧~今天就让我们深入研 ...