SRM-697-DIV2

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 = 2520, sum = 0;
  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.

____+++++____

  

时间: 2024-08-29 23:07:20

SRM-697-DIV2的相关文章

[TC SRM 697 div1 lev1] DivisibleSetDiv1

Tutorial:https://apps.topcoder.com/wiki/display/tc/SRM+697#DivisibleSetDiv1 Note:证明过程值得一看. 主要内容:寻找[x1,x2,...,xn]使得满足bi * xi >= S - xi,其中S = x1 + x2 + ... + xn.

topcoder SRM 618 DIV2 WritingWords

只需要对word遍历一遍即可 int write(string word) { int cnt = 0; for(int i = 0 ; i < word.length(); ++ i){ cnt+=word[i]-'A'+1; } return cnt; } topcoder SRM 618 DIV2 WritingWords,布布扣,bubuko.com

topcoder SRM 618 DIV2 MovingRooksDiv2

一开始Y1,Y2两个参数看不懂,再看一遍题目后才知道,vector<int>索引代表是行数,值代表的是列 此题数据量不大,直接深度搜索即可 注意这里深度搜索的访问标识不是以前的索引和元素,而是一个交换元素后的整个状态vector<int>,这样可以避免重复元素的搜索 set<vector<int> > visit; bool flag; void dfs(vector<int>& src, vector<int>& d

topcoder SRM 618 DIV2 LongWordsDiv2

此题给出的条件是: (1)word的每个字母都是大写字母(此条件可以忽略,题目给的输入都是大写字母) (2) 相等字符不能连续,即不能出现AABC的连续相同的情况 (3)word中不存在字母组成xyxy的形式,即不存在第一个字符和第3个字符相等同时第2个字符和第4个字符相等的情况 对于第(2)种情况,只需要考虑word[i]!=word[i-1]即可 对于第(3)种情况,用一个4重循环遍历每种可能的情况,然后第一个字符和第3个字符相等同时第2个字符和第4个字符相等,则输出“DisLikes”即可

TOPCODER SRM 686 div2 1000

// TOPCODER SRM 686 div2 1000 Problem Statement 给出一个至多长 100 的字符串,仅包含 ( 和 ),问其中有多少个不重复的,合法的括号子序列. 子序列可以不连续:合法即括号序列的合法:答案模 1,000,000,007. Examples "(())(" Returns: 2 Correct non-empty bracket subsequences are "()" and "(())". &

SRM 628 DIV2

250  想想就发现规律了. 500  暴力,括号匹配. 1000 给一个f数组,如果i存在,那么f[i]也得存在,问这样的集合有多少种. 先拓扑一下,dp[i] = mul(dp[son]+1)最后环里面的元素的乘积是结果. #include <iostream> #include <cstdio> #include <string> #include <algorithm> #include <stdlib.h> #include <v

topcoder srm 628 div2 250 500

做了一道题,对了,但是还是掉分了. 第二道题也做了,但是没有交上,不知道对错. 后来交上以后发现少判断了一个条件,改过之后就对了. 第一道题爆搜的,有点麻烦了,其实几行代码就行. 250贴代码: 1 #include <iostream> 2 #include <cstring> 3 #include <queue> 4 #include <cmath> 5 #include <cstdio> 6 #include <algorithm&g

topcoder SRM 619 DIV2 GoodCompanyDivTwo

注意题目给的最后一句话,如果部门任何employee都做不同类型的工作,则这个部门是一个diverse,题目是计算department的diverse数 读起来感觉有点别扭,英语没学好的原因 int countGood(vector <int> superior, vector <int> workType) { int res = 0; for(int i = 0 ; i < superior.size(); ++ i){ set<int> department

Topcoder SRM 619 DIv2 500 --又是耻辱的一题

这题明明是一个简单的类似约瑟夫环的问题,但是由于细节问题迟迟不能得到正确结果,结果比赛完几分钟才改对..耻辱. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #define ll long long using namespace std; #define NN 370000 class Choo

Topcoder SRM 648 Div2 1000

Problem 给一个长度为N的字符串S,S只含有'A'.'B'.'C'三种元素.给定一个K,要求返回字符串S,使得S中恰好有K对pair(i,j)满足 0=<i<j<N,且 S[i]<S[j].若不存在,则返回空串. Limits Time Limit(ms): 2000 Memory Limit(MB): 256 N: [3, 30] K: [0, N*(N-1)/2 ] Solution 设S中含有n1个'A',n2个'B',n3个'C',设num=n1*n2+n1*n3+n