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. Coloring Brackets (区间DP)

    Once Petya read a problem about a bracket sequence. He gave it much thought but didn't find a soluti ...

  2. POJ2926-Requirements,曼哈顿距离。去掉绝对值符号暴力枚举所有情况,神薙!

                                                         Requirements 好吧,这题我实在想不到什么优化的方法,看了看讨论区,顺便膜拜了一下大 ...

  3. [codevs1050]棋盘染色 2

    [codevs1050]棋盘染色 2 试题描述 有一个5*N的棋盘,棋盘中的一些格子已经被染成了黑色,你的任务是对最少的格子染色,使得所有的黑色能连成一块. 输入 第一行一个整数N(<=100) ...

  4. mappedBy的作用

    mappedBy的意思就是"被映射",即mappedBy这方不用管关联关系,关联关系交给另一方处理 1.规律:凡是双向关联,mapped必设,因为根本都没必要在2个表中都存在一个外 ...

  5. [NOIP2003] 提高组 洛谷P1040 加分二叉树

    题目描述 设一个n个节点的二叉树tree的中序遍历为(1,2,3,…,n),其中数字1,2,3,…,n为节点编号.每个节点都有一个分数(均为正整数),记第i个节点的分数为di,tree及它的每个子树都 ...

  6. 【HDOJ6318】Swaps and Inversions(树状数组)

    题意: 给定一串数组,其中含有一个逆序对则需要花费x,交换相邻两个数需要花费y,输出最小花费. n<=1e5,-1e9<=a[i]<=1e9 思路: #include<cstd ...

  7. 2018 江苏省邀请赛 H

    题目链接 https://nanti.jisuanke.com/t/28872 解析 递推 直接套杜教板子 AC代码 #include <cstdio> #include <cstr ...

  8. 寒武纪camp Day1

    补题进度:8/10 A(组合计数) 题意: 一个人站在数轴原点,每秒有1/4概率向前走一步,1/4概率向后走一步,1/2概率不动,问t秒后在p位置的概率. t,p<=100000 分析: 枚举不 ...

  9. MySql基本数据类型(转)

    说明:通俗的理解:1字节的8位,即1byte=8bit,而这个1byte叫做长度范围,范围的算法是使用bit去求,比如8bit的长度范围是2的8次方,但是在数据库中的类型上是有区分有符号和无符号的,默 ...

  10. Import Items – Validation Multiple Languages Description

    ð  提交标准请求创建和更新物料,因语言环境与处理次序方式等因素,造成物料中英(更多语言)描述和长描述混乱刷新. 症状: >>> Submit Standard Open Inter ...