Moduli number system
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的更多相关文章
- 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 ...
- 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 ...
- The Stern-Brocot Number System(排序二进制)
The Stern-Brocot Number System Input: standard input Output: standard output The Stern-Brocot tree i ...
- 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 ...
- 为什么实数系里不存在最小正数?(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 ...
- POJ1023 The Fun Number System
题目来源:http://poj.org/problem?id=1023 题目大意: 有一种有趣的数字系统.类似于我们熟知的二进制,区别是每一位的权重有正有负.(低位至高位编号0->k,第i位的权 ...
- lightOJ 1172 Krypton Number System(矩阵+DP)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1172 题意:一个n进制(2<=n<=6)的数字,满足以下条件:(1)至少包 ...
- 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& ...
- UVa 11651 Krypton Number System DP + 矩阵快速幂
题意: 有一个\(base(2 \leq base \leq 6)\)进制系统,这里面的数都是整数,不含前导0,相邻两个数字不相同. 而且每个数字有一个得分\(score(1 \leq score \ ...
随机推荐
- C# ACM poj1005
大水题呀 public static void acm1005(int n, float[,] a) { float pi = 3.1415926f, rr; int years; ; i < ...
- 客官,您的 Flask 全家桶请收好
http://www.factj.com/archives/543.html Flask-AppBuilder - Simple and rapid Application buil ...
- Codevs 1001 舒适的路线 2006年 NOIP全国联赛提高组
1001 舒适的路线 2006年 时间限制: 2 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description Z小镇是一个景色宜人的地方,吸引来自各地的观 ...
- ifstream:incomplete type is not allowed
IntelliSense: incomplete type is not allowed ifstream inputFile; Need to add this: #include <fstr ...
- HDOJ 1024 Max Sum Plus Plus -- 动态规划
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1024 Problem Description Now I think you have got an ...
- POJ 1088 滑雪 -- 动态规划
题目地址:http://poj.org/problem?id=1088 Description Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当 ...
- scroll
var fScrollTopHeight = function(){ return document.documentElement&&document.documentElement ...
- mysql触发器的例子--插入前更新数据
本文介绍下,一个mysql触发器的例子,在数据插入前更新相关内容,有需要的朋友参考下. mysql触发器的例子,如下: view source print? 001 mysql> CREATE ...
- Android开发框架
AsyncHttpClient 它把HTTP所有的通信细节全部封装在了内部,我们只需要简单调用几行代码就可以完成通信操作 Universal-Image-Loader 它使得在界面上显示网络图片的操作 ...
- IntelliJ IDEA 14.x 创建工作空间与多个Java Web项目
以往的Eclipse.NetBeans等开发工具不同,IDEA的Project相当与Eclipse的Workspace,而Module相当于Project. 下边就给出Eclipse与IDEA的概念的 ...