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
  1. 3
  2. 1
  3. 2
  4. 3
  1. 1
  1. 4
  2. 2
  3. 2
  4. 2
  5. 2
  1. 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掉题之前不看题解,自己做!!!加油)

  1. #include<cstdio>
  2. #include<cstdlib>
  3. #include<iostream>
  4. #include<algorithm>
  5. using namespace std;
  6. const int maxn=;
  7. int p[maxn+],vis[maxn+],num[maxn*+],cnt;
  8. void getprime()
  9. {
  10. for(int i=;i<=maxn;i++){
  11. if(!vis[i]) p[++cnt]=i;
  12. for(int j=;j<=cnt&&i*p[j]<=maxn;j++){
  13. vis[i*p[j]]=;
  14. if(i%p[j]==) break;
  15. }
  16. }
  17. }
  18. int main()
  19. {
  20. getprime();
  21. int N,i,j,x;
  22. bool F=true;
  23. scanf("%d",&N);
  24. for(i=;i<=N;i++){
  25. scanf("%d",&x);
  26. if(x!=) F=false;
  27. for(j=;j<=cnt;j++){
  28. if(x%p[j]==){
  29. num[p[j]]++;
  30. while(x%p[j]==) x/=p[j];
  31. }
  32. }
  33. if(x>) num[x]++;
  34. }
  35. if(F) {
  36. printf("0\n");
  37. return ;
  38. }
  39. for(i=;i<=;i++)
  40. if(num[i]>){
  41. printf("infinity\n");
  42. return ;
  43. }
  44. for(i=;i<=;i++)
  45. if(num[i]==){
  46. printf("2\n");
  47. return ;
  48. }
  49. printf("1\n");
  50. return ;
  51. }

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. python003 Python3 基本数据类型

    Python3 基本数据类型Python 中的变量不需要声明.每个变量在使用前都必须赋值,变量赋值以后该变量才会被创建.在 Python 中,变量就是变量,它没有类型,我们所说的"类型&qu ...

  2. zoj 1240

    IBM Minus One Time Limit: 2 Seconds      Memory Limit: 65536 KB You may have heard of the book '2001 ...

  3. php 面向对象 (类 对象)

    //面向对象//什么是面向对象//面向过程//什么是对象?//一切皆是对象//类//由对象抽象化//造类//class Ren//{ //构造方法 - - 写不写都存在//类的初始化方法 //构造方法 ...

  4. HDU-1858-Max Partial Value I,有坑点,不难;

    Max Partial Value I Time Limit: 1000/5000 MS (Java/Others)    Memory Limit: 32768/65535 K (Java/Othe ...

  5. c++ 实现 key-value缓存数据结构

    c++ 实现 key-value缓存数据结构 概述 最近在阅读Memcached的源代码,今天借鉴部分设计思想简单的实现了一个keyvalue缓存. 哈希表部分使用了unordered_map,用于实 ...

  6. SSH移植

    1. 下载需要的源码,直接google最新的版本吧 mkdir -p ~/arm/fs ;mkdir -p ~/arm/source 下载zlib: zlib-1.2.3.tar.gz 下载ssl : ...

  7. WebLogic"域"的概念

    WebLogic Server中的域是逻辑上相关的一组 WebLogic Server 资源,可以作为一个单元进行管理.一个域中包含一台或多台 WebLogic Server,也可以包含 WebLog ...

  8. HDU 4115 Eliminate the Conflict(2-sat)

    HDU 4115 Eliminate the Conflict pid=4115">题目链接 题意:Alice和Bob这对狗男女在玩剪刀石头布.已知Bob每轮要出什么,然后Bob给Al ...

  9. 使用WIN32汇编语言实现一个基本windows窗体的过程分析

    一个常规的windows窗体一般都是一些一样的构造.你假设想要更改一些个性化的设置,你能够在这个一般的模板伤添砖加瓦.构造自己比較喜欢的类型.下边就分析一下一般的windows窗体的一般模板. 一. ...

  10. [LeetCode][Java] Subsets

    题目: Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a subset ...