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. Codeforces 513G1 513G2 Inversions problem [概率dp]

    转自九野:http://blog.csdn.net/qq574857122/article/details/43643135 题目链接:点击打开链接 题意: 给定n ,k 下面n个数表示有一个n的排列 ...

  2. 牛客网暑期ACM多校训练营(第九场) A题 FWT

    链接:https://www.nowcoder.com/acm/contest/147/A来源:牛客网 Niuniu has recently learned how to use Gaussian ...

  3. HDU3430 (置换群循环节+中国剩余定理)

    题意:给出n张牌,标号为1-n,然后给出两个序列,序列1表示序列1,2,3,4……,n洗一次牌后到达的,序列2表示目标序列,问初始序列按序列1的洗牌方式洗几次能到达序列2的情况,如果不能到达输出-1. ...

  4. java构造方法的特点和理解--三只坚果

    构造方法的特点:1.首先构造方法是基于类,名字必须与类的名字完全相同(构造方法一般是自己编写的类需要初始化)2.每个类都有一个默认的构造方法,既无参数又无返回值,其作用是使用new操作符创建新对象后初 ...

  5. 【TFS 2017 CI/CD系列 - 03】-- Release篇

    为Project创建Release必须要先创建Build,若还没有Build definition请看上一篇文章:[TFS 2017 CI/CD系列 - 02]-- Build篇 一.创建Releas ...

  6. Org-mode五分钟教程ZZZ

    Table of Contents 1 源起 2 简介 2.1 获取 org-mode 2.2 安装 3 基础用法 3.1 创建一个新文件 3.2 简单的任务列表 3.3 使用标题组织一篇文章 3.4 ...

  7. crontab使用简介

    crontab的配置文件: 前四行是用来配置crond任务运行的环境变量 第一行SHELL变量指定了系统要使用哪个shell,这里是bash 第二行PATH变量指定了系统执行命令的路径 第三行MAIL ...

  8. Android 自己定义UI文章汇总

    <Android ListView分类/分组效果 - 第一种实现方式> <Android ListView分类/分组效果 - 另外一种实现方式> <Android Lis ...

  9. linux迁移至固态硬盘全过程

    自从台式机上用上固态硬盘后,就再也受不了笔记本上的5400转的机械硬盘了,所以这次又买了块固态硬盘打算装到笔记本上. 笔记本里装的是Ubuntu 14.04 + Win7双系统,Win7主要偶尔运行一 ...

  10. Mockito的简单使用方法演示样例

    Mockito是一个流行的Mocking框架.它使用起来简单,学习成本非常低.并且具有非常简洁的API,測试代码的可读性非常高.因此它十分受欢迎,用 户群越来越多.非常多的开源的软件也选择了Mocki ...