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. jxl和poi处理excel之比较

    功能需求是根据客户提供的excel模板,程序动态填充其中的一些数据.该模板包含大量的宏,刚拿到的时候头都要晕了,本人虽天天和电脑打交道,但是excel咱不是高手啊,什么宏,之前光听过,听着都觉得是高级 ...

  2. 求给定数据中最小的K个数

    public class MinHeap { /* * * Top K个问题,求给定数据中最小的K个数 * * 最小堆解决:堆顶元素为堆中最大元素 * * * */ private int MAX_D ...

  3. python制作安装包(setup.py)

    1.制作setup.py from distutils.core import setup setup(name='Myblog', version='1.0', description='My Bl ...

  4. Python的传值和传址与copy和deepcopy

    1.传值和传址 传值就是传入一个参数的值,传址就是传入一个参数的地址,也就是内存的地址(相当于指针).他们的区别是如果函数里面对传入的参数重新赋值,函数外的全局变量是否相应改变,用传值传入的参数是不会 ...

  5. Asp.Net检查HTML是否闭合以及自动修复

    1.htmlCheck类 using System; using System.Collections.Generic; using System.Text; using System.Collect ...

  6. Intel HEX file结构

    https://en.wikipedia.org/wiki/Intel_HEX 1, Intel Hex每行的组成 开始标志+Byte数+地址+数据类型+数据+Checksum 2, 开始标志 冒号: ...

  7. AVQueuePlayer,备用

    想要视频一个接一个的无缝连续播放么?还在用二逼的mpmovieplayercontroller么?太out了!本大仙介绍一个可以实现无缝连续播放视频的东西-------AVQueuePlayer ! ...

  8. HDU 1560 DNA sequence (IDA* 迭代加深 搜索)

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1560 BFS题解:http://www.cnblogs.com/crazyapple/p/321810 ...

  9. 数据结构———重载与模板(C++)

    相关信息 /* * * @subject 数据结构 * @author 信管1142班 201411671210 JJ lai * * @program 模板与重载 * */ 实验一: /* 要求如下 ...

  10. Python属性、方法和类管理系列之----元类

    元类的介绍 请看位于下面网址的一篇文章,写的相当好. http://blog.jobbole.com/21351/ 实例补充 class Meta(type): def __new__(meta, c ...