*[topcoder]LCMSetEasy
http://community.topcoder.com/stat?c=problem_statement&pm=13040
DFS集合全排列+LCM和GCD。但事实上,有更简单的算法,列在下面,就是观察到不能整除x的对我们无效。
#include <vector>
#include <iostream>
using namespace std; class LCMSetEasy {
public:
string include(vector <int> S, int x) {
vector<int> vec;
if (includeRe(S, x, 0, vec))
return "Possible";
else
return "Impossible";
} bool includeRe(const vector<int> &S, int x, int i, vector<int> &vec) {
if (i == S.size()) {
int r = lcm(vec);
if (r == x)
return true;
return false;
}
vec.push_back(S[i]);
bool res = includeRe(S, x, i + 1,vec);
vec.pop_back();
if (res)
return true;
res = includeRe(S, x, i + 1,vec);
return res;
} int lcm(vector<int> vec) {
if (vec.size() == 0)
return 1;
int tmp = vec[0];
for (int i = 1; i < vec.size(); i++) {
tmp = lcm(tmp, vec[i]);
}
return tmp;
} int lcm(int a, int b) {
int c = gcd(a, b);
return a * b /c;
} int gcd(int a, int b) {
if (a % b == 0)
return b;
int tmp = a % b;
return gcd(b, tmp);
}
};
string include(vector <int> S, int x) {
int r = 1;
for (int i = 0; i < S.size(); i++) {
if (x % S[i] == 0) {
r = lcm(r, S[i]);
}
}
if (r == x)
return "Possible";
else
return "Impossible";
}
*[topcoder]LCMSetEasy的更多相关文章
- TopCoder kawigiEdit插件配置
kawigiEdit插件可以提高 TopCoder编译,提交效率,可以管理保存每次SRM的代码. kawigiEdit下载地址:http://code.google.com/p/kawigiedit/ ...
- 记第一次TopCoder, 练习SRM 583 div2 250
今天第一次做topcoder,没有比赛,所以找的最新一期的SRM练习,做了第一道题. 题目大意是说 给一个数字字符串,任意交换两位,使数字变为最小,不能有前导0. 看到题目以后,先想到的找规律,发现要 ...
- TopCoder比赛总结表
TopCoder 250 500 ...
- Topcoder几例C++字符串应用
本文写于9月初,是利用Topcoder准备应聘时的机试环节临时补习的C++的一部分内容.签约之后,没有再进行练习,此文暂告一段落. 换句话说,就是本文太监了,一直做草稿看着别扭,删掉又觉得可惜,索性发 ...
- TopCoder
在TopCoder下载好luncher,网址:https://www.topcoder.com/community/competitive%20programming/ 选择launch web ar ...
- TopCoder SRM 596 DIV 1 250
body { font-family: Monospaced; font-size: 12pt } pre { font-family: Monospaced; font-size: 12pt } P ...
- 求拓扑排序的数量,例题 topcoder srm 654 div2 500
周赛时遇到的一道比较有意思的题目: Problem Statement There are N rooms in Maki's new house. The rooms are number ...
- TopCoder SRM 590
第一次做TC,不太习惯,各种调试,只做了一题...... Problem Statement Fox Ciel is going to play Gomoku with her friend ...
- Topcoder Arena插件配置和训练指南
一. Arena插件配置 1. 下载Arena 指针:http://community.topcoder.com/tc?module=MyHome 左边Competitions->Algorit ...
随机推荐
- JAVA多线程学习2--线程同步
一.线程同步介绍 同步:就是协同步调,按照预定的先后顺序执行.比如:你说完我再说. 线程同步:访问同一个共享资源的时候多个线程能够保证数据的安全性.一致性. 二.JAVA中实现线程同步的方法 实现进程 ...
- 使用 PHP 验证表单数据
使用 PHP 验证表单数据 首先我们对用户所有提交的数据都通过 PHP 的 htmlspecialchars() 函数处理. 当我们使用 htmlspecialchars() 函数时,在用户尝试提交以 ...
- js各类共用方法
function GetParameterValueByName(parametername) { var reg = new RegExp("(^|&)" + param ...
- Django模版进阶
# -*- coding: utf-8 -*-from django.shortcuts import renderdef home(request): string = "测试" ...
- Spark 大数据平台 Introduction part 2 coding
Basic Functions sc.parallelize(List(1,2,3,4,5,6)).map(_ * 2).filter(_ > 5).collect() *** res: Arr ...
- apache配置VirtualHost(windows)
以下方式适合原生 Apache, XAMPP 和WAMP 套件. 1. 打开目录 {Apache2 安装目录}\conf\extra\, 找到 httpd-vhosts.conf 文件. 2. 仿照例 ...
- PAT乙级真题1008. 数组元素循环右移问题 (20)
原题: 1008. 数组元素循环右移问题 (20) 时间限制400 ms内存限制65536 kB 一个数组A中存有N(N>0)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(M&g ...
- 在Visual Studio 2010 中创建类库(dll)
创建类库 选择"文件"->新建->项目->Visual C# ->类库,输入名称,选择位置,单击确定 浏览解决方案资源管理器,可以看到两个C#类,第一个是A ...
- 微软职位内部推荐-SDE2 (Windows - Audio)
微软近期Open的职位: SDE2 (Windows - Audio) Windows Partner Enablement team in Operating System Group is loo ...
- linux消息队列的使用
消息队列 *消息队列是内核地址空间中的内部链表,通过内核在各个进程之间传递的内容.消息顺序发送到消息队列中,每个消息队列都有IPC标识符唯一地进行标识. msgbuf结构 struct msgbuf{ ...