题目: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. JavaScript学习二

    2019-05-30 15:08:24 加油,这几天在赶高数,都…… <!DOCTYPE html> <html> <head> <script type=& ...

  2. centos7安装python3.7和ipython

    一.centos7为刚安装的 1)配置yum源和epel源 采用国内源 查看yum的配置文件 (里面的镜像网址)是否ping的通 全部更改成 国内的 yum .epel源 在图中位置 下载相应的 re ...

  3. var的变量提升的底层原理是什么?

    原理:JS引擎的工作方式是①先解析代码,获取所有被声明的变量:②然后在运行.也就是专业来说是分为预处理和执行两个阶段. 变量提升的定义:所有变量的声明语句都会被提升到代码头部,这就是变量提升. 例如: ...

  4. .htaccess的基本用法与介绍

    ●自定义错误页 .htaccess的一个应用是自定义错误页面,这将使你可以拥有自己的.个性化的错误页面(例如找不到文件时),而不是你的服务商提供的错误页或没有任何页面.这会让你的网站在出错的时候看上去 ...

  5. 向properties文件中写入信息(针对获取properties文件失败的总结)

    前段时间项目需要将某个属性动态的写入项目发布路径下的properties文件中;但是实际发布时发现找不到maven项目resource路径下的project.properties文件,调试多次代码如下 ...

  6. nvcc fatal : Unsupported gpu architecture 'compute_11'

    使用VS编译OpenCV编译源代码时候,对Cmake生成的工程文件编译,会出现 nvcc fatal : Unsupported gpu architecture 'compute_11'  问题.原 ...

  7. gitlab变更邮箱后发送邮件报SSLError错误

    测试发送邮件: gitlab-rails console Notify.test_email('test666@example.com', 'Message Subject', 'Message Bo ...

  8. 怎么获取自定义核算项目里某一个类型的数据:做f7

    在BOS里加一个F7字段,关联物料或其他可以选到的基础资料.保存后先别发布    切换到BOS透视图,打到对应的.relation文件,修改supplierEntity,原来是指定物料的实体,改成自定 ...

  9. 【剑指Offer】64、滑动窗口的最大值

      题目描述:   给定一个数组和滑动窗口的大小,找出所有滑动窗口里数值的最大值.例如,如果输入数组{2,3,4,2,6,2,5,1}及滑动窗口的大小3,那么一共存在6个滑动窗口,他们的最大值分别为{ ...

  10. C#第十节课

    类 using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Thr ...