Div2 Medium: DivisibleSetDiv2

Problem Statement

     You are given a vector <int> b containing a sequence of n positive integers: b[0], ..., b[n-1]. We are now looking for another sequence a[0], ..., a[n-1]. This sequence should have the following properties:

  • Each a[i] should be a number of the form 2^x[i] where x[i] is some positive integer. In other words, each a[i] is one of the numbers 2, 4, 8, 16, ...
  • For each i, the value a[i]^b[i] (that is, a[i] to the power b[i]) should be divisible by P, where P is the product of all a[i].

Determine whether there is at least one sequence with the desired properties. Return "Possible" (quotes for clarity) if such a sequence exists and "Impossible" otherwise.

Definition

    
Class: DivisibleSetDiv2
Method: isPossible
Parameters: vector <int>
Returns: string
Method signature: string isPossible(vector <int> b)
(be sure your method is public)

Limits

    
Time limit (s): 2.000
Memory limit (MB): 256
Stack limit (MB): 256

Constraints

- b will contain between 1 and 50 elements, inclusive.
- Each element in b will be between 1 and 10, inclusive.

Examples

0)  
    
{3,2}
Returns: "Possible"
One valid sequence is the sequence {2, 2}. That is, a[0] = a[1] = 2. Clearly, each a[i] is a power of two not smaller than 2. The product of all a[i] is 2*2 = 4. Both a[0]^b[0] = 2^3 = 8 and a[1]^b[1] = 2^2 = 4 are divisible by 4.
1)  
    
{3,3,3}
Returns: "Possible"
Here, one valid sequence is {2, 2, 2}.
2)  
    
{1,10}
Returns: "Impossible"
Suppose that a[0] = x and a[1] = y. The value a[0]^b[0] = x^1 should be divisible by x*y. This is only possible for y = 1. However, 1 is not a positive power of two, so we cannot have a[1] = 1.
3)  
    
{2, 3, 10}
Returns: "Possible"
One valid sequence is {8, 4, 2}.
4)  
    
{7,10,4,6,3}
Returns: "Possible"
 
5)  
    
{9,9,9,9,9,9,9,9,9}
Returns: "Possible"
 
6)  
    
{3,4,5,6,7}
Returns: "Impossible"
 

  This is a mathematical problem which is very simple to implement but hard to prove. It turns out that the answer is "Possible" if and only if ∑i=0n−11bi≤1∑i=0n-11bi≤1. You can find a detailed proof below.

  We are asked if there exists a sequence of powers of two (a0,a1,...,an−1)(a0,a1,...,an-1) that for every i:

  Finally, we should avoid losing accuracy in order to use the perfect formula. Casuing the range of bi is [1, 10], we can multiply a constant C which equal to LCM(1,...,10).

public string isPossible(vector<int> b) {
int LCM = , sum = ;
for (int bi : b)
sum += LCM / bi; // sum up 1/b multiplied by LCM to avoid floats
return (sum <= LCM) ? "Possible" : "Impossible";
}

  Recently, I had met two mathematical problems. The last one is hihoCoder_5 using log to get the maximum value.

____+++++____

  

SRM-697-DIV2的更多相关文章

  1. SRM 657 DIV2

    -------一直想打SRM,但是感觉Topcoder用起来太麻烦了.题目还是英文,不过没什么事干还是来打一打好了.但是刚注册的号只能打DIV2,反正我这么弱也只适合DIV2了.. T1: 题目大意: ...

  2. Topcoder Srm 673 Div2 1000 BearPermutations2

    \(>Topcoder \space Srm \space 673 \space Div2 \space 1000 \space BearPermutations2<\) 题目大意 : 对 ...

  3. Topcoder Srm 671 Div2 1000 BearDestroysDiv2

    \(>Topcoder \space Srm \space 671 \space Div2 \space 1000 \space BearDestroysDiv2<\) 题目大意 : 有一 ...

  4. 记第一次TopCoder, 练习SRM 583 div2 250

    今天第一次做topcoder,没有比赛,所以找的最新一期的SRM练习,做了第一道题. 题目大意是说 给一个数字字符串,任意交换两位,使数字变为最小,不能有前导0. 看到题目以后,先想到的找规律,发现要 ...

  5. Topcoder srm 632 div2

    脑洞太大,简单东西就是想复杂,活该一直DIV2; A:水,基本判断A[I]<=A[I-1],ANS++; B:不知道别人怎么做的,我的是100*N*N;没办法想的太多了,忘记是连续的数列 我们枚 ...

  6. SRM 638 Div2

    2333... 因为TC过少的参与者.加上不断fst 我掉了div2该. 幸运的是完成的背div1该.. 250 水的问题 500 水的问题.. 直接bfs扩展即可了 注意判重.  我还用康托展开了真 ...

  7. SRM 592 DIV2 报告

    昨天下午查看邮箱,看到了topcoder的SRM比赛通知和cf的比赛通知,当时什么也不想做,心里空荡荡的,忽然就想参加一下,试试看.吃完晚饭回来一看,就剩十几分钟了,匆忙把平台下了,就开始等待比赛开始 ...

  8. SRM 670 div2 A B C div1 A(贪心,子问题合并)

    A Cdgame brute force... B Drbalance 贪心,每次选最前面的-变成+,相当于后面所有的负值+2. C Treestrat 考虑集中去抓一个Red Token,以这个To ...

  9. topcpder SRM 664 div2 A,B,C BearCheats , BearPlays equalPiles , BearSorts (映射)

    A题,熊孩子测视力,水题,题意就是判断一下两个数对应位不相同的数字有多少个. #include<bits/stdc++.h> using namespace std; class Bear ...

  10. topcoder SRM 628 DIV2 BracketExpressions

    先用dfs搜索所有的情况,然后判断每种情况是不是括号匹配 #include <vector> #include <string> #include <list> # ...

随机推荐

  1. 【OpenCV, MFC】利用MFC和OpenCV通过系统对话框打开和保存图片

    打开图片: void CImageProDlg::OnImageopen() { // TODO: 在此添加命令处理程序代码 Invalidate(); CFileDialog dlg(TRUE, N ...

  2. 百练4103:踩方格(DFS)

    描述 有一个方格矩阵,矩阵边界在无穷远处.我们做如下假设:a.    每走一步时,只能从当前方格移动一格,走到某个相邻的方格上:b.    走过的格子立即塌陷无法再走第二次:c.    只能向北.东. ...

  3. Sublime text如何设置快捷键让编写的HTML文件在浏览器预览?

      STEP 1 Tools->Build System->New Build System STEP 2<img src="https://pic3.zhimg.com/ ...

  4. 【Codeforces 246D】Colorful Graph

    [链接] 我是链接,点我呀:) [题意] 让你找到所有和x颜色的点中,和该颜色的点颜色不同的相邻的点的个数(重复颜色算一次) 求出哪种颜色的所要求的点的数量最多. [题解] 对于每一条边只会被查到两次 ...

  5. 【Codeforces 484A】Bits

    [链接] 我是链接,点我呀:) [题意] 让你求出l~r当中二进制表示1的个数最多的数x [题解] 最多有64位 我们可以从l开始一直增大到r 怎么增大? 找到l的二进制表示当中0所在的位置 假设i这 ...

  6. windows下如何正确使用Jconsole远程连接linux主机上的JVM

    https://www.aliyun.com/jiaocheng/589230.html

  7. Problem 2669

    Romantic Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total S ...

  8. linux top进程状态D

    什么是D状态 运行在KVM虚拟机里的一些进程突然出了问题,这些出了问题的进程无法用kill杀掉,使用ps可以看到这些进程处于D状态: [build@kbuild-john ~]$ ps -a -o p ...

  9. SQL SERVE BASE

    http://www.cnblogs.com/chillsrc/category/49632.html

  10. 自然语言处理中的Attention Model:是什么及为什么

    /* 版权声明:能够随意转载.转载时请标明文章原始出处和作者信息 .*/ author: 张俊林 要是关注深度学习在自然语言处理方面的研究进展,我相信你一定听说过Attention Model(后文有 ...