A number system with moduli is defined by a vector of k moduli, [m1,m2, ···,mk].

The moduli must be pairwise co-prime, which means that, for any pair of moduli, the only common factor is 1.

In such a system each number n is represented by a string "-x1--x2-- ... --xk-" of its residues, one for each modulus. The product m1 ... mk must be greater than the given number n which is to be converted in the moduli number system.

For example, if we use the system [2, 3, 5] the number n = 11 is represented by "-1--2--1-",
the number n = 23 by "-1--2--3-". If we use the system [8, 7, 5, 3] the number n = 187 becomes "-3--5--2--1-".

You will be given a number n (n >= 0) and a system S = [m1,m2, ···,mk] and you will return a string "-x1--x2-- ...--xk-" representing the number n in the system S.

If the moduli are not pairwise co-prime or if the product m1 ... mk is not greater than n, return "Not applicable".

Examples:

fromNb2Str(11 [2,3,5]) -> "-1--2--1-"

fromNb2Str(6, [2, 3, 4]) -> "Not applicable", since 2 and 4 are not coprime

fromNb2Str(7, [2, 3]) -> "Not applicable" since 2 * 3 < 7

fromNb2Str 187 [8,7,5,3] -> "-3--5--2--1-"
fromNb2Str 6 [2, 3, 4] -> "Not applicable", since 2 and 4 are not coprime
fromNb2Str 7 [2, 3] -> "Not applicable", since 2 * 3 < 7
public static class Kata
{
public static String fromNb2Str(int n, int[] sys)
{
string str = "Not applicable";
bool flag = CoPrimeArray(sys);
if (flag)
{
IEnumerable<int> list = sys.Select(x => n % x);
int result = sys.Aggregate(, (sum, y) => sum * y);
if (result > n)
{
str = string.Join(string.Empty, list.Select(x => string.Format("-{0}-", x)));
}
}
return str;
} public static bool CoPrimeArray(int[] array)
{
bool coPrime = false;
int max = array.Max();
int sqrt = Convert.ToInt32(Math.Floor(Math.Sqrt(max)));
bool[] tempArray = new bool[max + ];
tempArray = tempArray.Select(x => x = true).ToArray();
int prime = ;//质数,从最小的开始
IEnumerable<int> dividePrime = array.Where(x => x % prime == );//获取能够被质数整除的数字的集合
while (true)
{
if (dividePrime.Count() > )
{
//这里的判断涉及到了Linq的延迟加载,随着prime的改变,每一次的dividePrime是不同的
break;
}
//被除数/除数=商
for (int i = prime; i <= sqrt; i++)
{
for (int j = i; j * i < max; j++)
{
//j*i这个位置的数据可以被i整除
if (tempArray[j * i])
{
tempArray[j * i] = false;
}
}
}
while (true)
{
prime++;
if (prime == max)
{
//除数已经达到最大值,说明数组里面的所有数字互质
coPrime = true;
break;
}
else
{
if (tempArray[prime])
{
//说明没有被前面prime个数字整除过,
//假如prime是3的话,并且符合的话,说明3没有被2整除过
//假如prime是7的话,并且符合的话,说明7没有被2到6的数字整除过
break;
}
}
}
if (coPrime)
{
break;
}
}
return coPrime;
}
}

Moduli number system的更多相关文章

  1. Find n‘th number in a number system with only 3 and 4

    这是在看geeksforgeeks时看到的一道题,挺不错的,题目是 Given a number system with only 3 and 4. Find the nth number in th ...

  2. F - The Fun Number System(第二季水)

    Description In a k bit 2's complement number, where the bits are indexed from 0 to k-1, the weight o ...

  3. The Stern-Brocot Number System(排序二进制)

    The Stern-Brocot Number System Input: standard input Output: standard output The Stern-Brocot tree i ...

  4. POJ 1023 The Fun Number System

    Description In a k bit 2's complement number, where the bits are indexed from 0 to k-1, the weight o ...

  5. 为什么实数系里不存在最小正数?(Why the smallest positive real number doesn't exist in the real number system ?)

    We define the smallest positive real number as the number which is explicitly greater than zero and ...

  6. POJ1023 The Fun Number System

    题目来源:http://poj.org/problem?id=1023 题目大意: 有一种有趣的数字系统.类似于我们熟知的二进制,区别是每一位的权重有正有负.(低位至高位编号0->k,第i位的权 ...

  7. lightOJ 1172 Krypton Number System(矩阵+DP)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1172 题意:一个n进制(2<=n<=6)的数字,满足以下条件:(1)至少包 ...

  8. uva 10077 - The Stern-Brocot Number System

    想法: 初始化三個數L=0/1, M=1/1, R=1/0,設輸入的分數為a: 如果a<M,那麼要往左邊走,    R = M;    M = (L分子+M分子)/(L分母+M分母); 如果a& ...

  9. UVa 11651 Krypton Number System DP + 矩阵快速幂

    题意: 有一个\(base(2 \leq base \leq 6)\)进制系统,这里面的数都是整数,不含前导0,相邻两个数字不相同. 而且每个数字有一个得分\(score(1 \leq score \ ...

随机推荐

  1. cocos2d-x实战 C++卷 学习笔记--第4章 win32平台下中文乱码问题

    前言: 将GBK编码的字符串转为UTF-8编码.(通俗点说就是解决中文乱码问题) 简要介绍: 在Win32平台下通过 log 输出中文字符时,会出现中文乱码问题.同样的代码在 ios 和 Androi ...

  2. 9月18日,SQL学习基础1

    数据库管理和应用 Oltp是小型的管理,OLAP是大型的管理 开发的内容如触发器 数据库管理系统(Database Management System,简称为DBMS)是位于用户与操作系统之间的一层数 ...

  3. javascript 第28节 jQuery事件、迭代、样式

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. HDU 4430 Yukari's Birthday(二分)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4430 题目大意:给定n个蜡烛,围绕蛋糕的中心插同心圆,从里往外分别是第1圈.第2圈....第r圈,第 ...

  5. GNU glibc

    在线G-lib-c(GNU C Library库)网站 参考: 1.bitsToTypes

  6. hdu 1715 大菲波数(高精度数)

    Problem Description Fibonacci数列,定义如下: f(1)=f(2)=1 f(n)=f(n-1)+f(n-2) n>=3. 计算第n项Fibonacci数值. Inpu ...

  7. C#生成二维码名片

    摘自<31天学会CRM项目开发<C#编程入门级项目实战>> 本例将使用ThoughtWorks.QRCode类库生成二维码名片.正式编码前,先了解一下什么是vCard?它是一种 ...

  8. GCC编译器入门

    GCC(GNU Compiler Collection,GNU编译器套件),是由 GNU 开发的编程语言编译器.它是以GPL许可证所发行的自由软件,也是 GNU计划的关键部分.GCC原本作为GNU操作 ...

  9. A-Frame 简介03

    如果你想开始使用A-Frame可以通过以下几种方式: Play with CodePen Grab the Boilerplate Include the JS Build Install from ...

  10. DIV CSS布局中position属性用法深入探究

    本文向大家描述一下DIV CSS布局中的position属性的用法,position属性主要有四种属性值,任何元素的默认position的属性值均是static,静态.这节课主要讲讲relative( ...