题目:http://community.topcoder.com/stat?c=problem_statement&pm=10554&rd=15855

符合条件的集中非1的元素个数是非常少的,能够用回溯加剪枝。实际执行速度非常快。

代码:

#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <iostream>
#include <sstream>
#include <iomanip> #include <bitset>
#include <string>
#include <vector>
#include <stack>
#include <deque>
#include <queue>
#include <set>
#include <map> #include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <cstring>
#include <ctime>
#include <climits>
using namespace std; #define CHECKTIME() printf("%.2lf\n", (double)clock() / CLOCKS_PER_SEC)
typedef pair<int, int> pii;
typedef long long llong;
typedef pair<llong, llong> pll;
#define mkp make_pair
#define FOREACH(it, X) for(__typeof((X).begin()) it = (X).begin(); it != (X).end(); ++it) /*************** Program Begin **********************/ class Subsets {
public:
vector <int> numbers;
int ones_cnt;
int res;
int nextdiff[1005];
void backtrack(int sum, int prod, int pos)
{
// add
int cur = numbers[pos];
int next_sum = sum + cur;
int next_prod = prod * cur;
if (next_sum + ones_cnt > next_prod) {
res += next_sum + ones_cnt - next_prod;
if (pos + 1 < numbers.size()) {
backtrack(next_sum, next_prod, pos + 1);
}
} // not add
if (nextdiff[pos] < numbers.size()) {
backtrack(sum, prod, nextdiff[pos]);
}
} int findSubset(vector <int> numbers) {
sort(numbers.begin(), numbers.end());
this->numbers = numbers;
int n = numbers.size(); for (int i = 0; i < n; i++) {
nextdiff[i] = n;
for (int j = i + 1; j < n; j++) {
if (numbers[i] != numbers[j]) {
nextdiff[i] = j;
break;
}
}
} ones_cnt = count(numbers.begin(), numbers.end(), 1);
res = max(ones_cnt - 1, 0);
if (ones_cnt < n) {
backtrack(0, 1, ones_cnt);
} return res;
} }; /************** Program End ************************/

SRM 622 D2L3: Subsets, math, backtrack的更多相关文章

  1. SRM 621 D2L3: MixingColors, math

    题目:http://community.topcoder.com/stat? c=problem_statement&pm=10409&rd=15854 利用高斯消元求线性空间的基,也 ...

  2. SRM 620 D2L3: RandomGraph, dp

    称号:http://community.topcoder.com/stat? c=problem_statement&pm=13143&rd=15853 參考:http://apps. ...

  3. 90. Subsets II (Back-Track, DP)

    Given a collection of integers that might contain duplicates, nums, return all possible subsets. Not ...

  4. topcoder SRM 622 DIV2 FibonacciDiv2

    关于斐波那契数列,由于数据量比较小, 直接打表了,代码写的比较戳 #include <iostream> #include <vector> #include <algo ...

  5. topcoder SRM 622 DIV2 BoxesDiv2

    注意题目这句话,Once you have each type of candies in a box, you want to pack those boxes into larger boxes, ...

  6. SRM 624 D2L3: GameOfSegments, 博弈论,Sprague–Grundy theorem,Nimber

    题目:http://community.topcoder.com/stat?c=problem_statement&pm=13204&rd=15857 这道题目须要用到博弈论中的经典理 ...

  7. SRM 628 D1L3:DoraemonPuzzleGame,math,后市展望,dp

    称号:c=problem_statement&pm=13283&rd=16009">http://community.topcoder.com/stat?c=probl ...

  8. 78. Subsets (Back-Track, DP)

    Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a subset must ...

  9. Cognition math based on Factor Space (2016.05)

    Cognition math based on Factor Space Wang P Z1, Ouyang H2, Zhong Y X3, He H C4 1Intelligence Enginee ...

随机推荐

  1. Visual Studio q启动卡顿

    在开发人员CMD下面执行 Devenv.exe /ResetSettings ,然后顺利打开 总发现vs2015经常把cpu给占满了,导致电脑卡的不要不要的.这是CodeLens引起的,因为装了VAs ...

  2. office 2010 破解

    使用Rearm命令激活延迟重置Office 20101.安装Offcie 2010 安装Offcie 2010,默认30天的试用期,这里要注意,上文提供的Office 2010是零售版,所以没有序列号 ...

  3. C# Area 双重路由如何写

    在WebApi项目里面 一般除了接口, 还有管理端...一些乱七八糟的,你想展示的东西, 一种做法是分开写: 比如管理后台一个项目, 然后接口一个, 然后页面一个, 其实这样做也可以,但是这么做, 无 ...

  4. swift-delegate(代理)或者block传值

    1:delegate或者block传值 import UIKit class ViewController: UIViewController,TestDelegatePassValueDelegat ...

  5. DeltaFish 校园物资共享平台 第三次小组会议

    一.想法 娄雨禛: 网页底层开发转移到后端,快速建站,效率高. 可以依照模板进行仿制. 可以考虑只进行页面设计. 但是出现问题不会调试. 所以自己写源码,做出一个大致的样子. 二.上周进度汇报 齐天杨 ...

  6. Java code List Map, HashMap, JSON parser snippet

    package com.newegg.ec.solr.eventsalestoreservice.tuple; import kafka.message.MessageAndMetadata; pub ...

  7. Excel的用到的常规的技巧

    这几天在做各种发票的报表,好几百的数据当然离不开EXCel,自己又是个白班,就记录下啦! EXCEL 判断某一单元格值是否包含在某一列中 就在Excel的表格中加入这个函数:=IF(ISERROR(V ...

  8. 用js制作一个计算器

    使用js制作计算器 <!doctype html> <html lang="en"> <head> <meta charset=" ...

  9. 工作中总结的经验之git篇

    不要以为你会git,你要知道,git不是只有commit和push 由于系统分析与设计的期末Project需要团队合作开发,因此在这里想谈谈GitHub团队项目合作开发的流程: 项目创建 项目负责人在 ...

  10. Ubuntu 16.04安装和卸载软件命令

    安装软件 apt-get install softname1 softname2 softname3…… 卸载软件 apt-get remove softname1 softname2 softnam ...