USACO section 2.2 Subset Sums(DP,背包)

KNN——癌症数据集实验 一、定义 邻近算法,或者说K最近邻(kNN,k-NearestNeighbor)分类算法是数据挖掘分类技术中最简单的方法之一。所谓K最近邻,就是k个最近的邻居的意思,说的是每个样本都可以用它最接近的k个邻居来代表。 二、算法原理 三、实验(预测癌症) import random import csv #读取数据 with open('Prostate_Cancer.csv','r... 阅读详情
Subset Sums JRM

For many sets of consecutive integers from 1 through N (1 <= N <= 39), one can partition the set into two sets whose sums are identical.

For example, if N=3, one can partition the set {1, 2, 3} in one way so that the sums of both subsets are identical:

  • {3} and {1,2}

This counts as a single partitioning (i.e., reversing the order counts as the same partitioning and thus does not increase the count of partitions).

If N=7, there are four ways to partition the set {1, 2, 3, ... 7} so that each partition has the same sum:

  • {1,6,7} and {2,3,4,5}
  • {2,5,7} and {1,3,4,6}
  • {3,4,7} and {1,2,5,6}
  • {1,2,4,7} and {3,5,6}

Given N, your program should print the number of ways a set containing the integers from 1 through N can be partitioned into two sets whose sums are identical. Print 0 if there are no such ways.

Your program must calculate the answer, not look it up from a table.

PROGRAM NAME: subset

INPUT FORMAT

The input file contains a single line with a single integer representing N, as above.

SAMPLE INPUT (file subset.in)

7

OUTPUT FORMAT

The output file contains a single line with a single integer that tells how many same-sum partitions can be made from the set {1, 2, ..., N}. The output file should contain 0 if there are no ways to make a same-sum partition.

SAMPLE OUTPUT (file subset.out)

4

 
 
具体看程序:
 
 
/*
ID:nealgav1
PROG:subset
LANG:C++
*/
#include<fstream>
#include<cstring>
using namespace std;
const int mm=2300;
long long f[60][mm];
long long dp(int x,int c)
{ memset(f,0,sizeof(f));
  f[1][0]=1;f[1][1]=1;
  for(int i=2;i<=x;i++)
  for(int j=0;j<=c;j++)
  if(j-i>=0)
  f[i][j]=f[i-1][j]+f[i-1][j-i];
  else f[i][j]=f[i-1][j];
  /** 容量为j,个数为i的方法数有两种情况,一是个数为i-1,容量就已经是j了,这时不用
  往里面放东西,另一种是当个数为i-1,容量为j-i;这时只要将个数i放入容量即为j;因此
  f[i][j]=f[i-1][j]+f[i-1][j-i];*/
  return f[x][c];
}
int main()
{ ifstream cin("subset.in");
  ofstream cout("subset.out");
  int m,ans;
  cin>>m;
  ans=((1+m)*m)/2;
  if(ans&1){cout<<"0\n";return 0;}///当和为奇数时不可能
  else ans/=2;
  cout<<dp(m,ans)/2<<"\n";///答案是一半和容量的1/2.
}

 
 
 
 
 
USER: Neal Gavin Gavin [nealgav1]
TASK: subset
LANG: C++

Compiling...
Compile: OK

Executing...
   Test 1: TEST OK [0.000 secs, 4412 KB]
   Test 2: TEST OK [0.000 secs, 4412 KB]
   Test 3: TEST OK [0.011 secs, 4412 KB]
   Test 4: TEST OK [0.011 secs, 4412 KB]
   Test 5: TEST OK [0.011 secs, 4412 KB]
   Test 6: TEST OK [0.011 secs, 4412 KB]
   Test 7: TEST OK [0.000 secs, 4412 KB]

All tests OK.

YOUR PROGRAM ('subset') WORKED FIRST TIME! That's fantastic -- and a rare thing. Please accept these special automated congratulations.

Here are the test data inputs:

------- test 1 ----
7
------- test 2 ----
15
------- test 3 ----
24
------- test 4 ----
31
------- test 5 ----
36
------- test 6 ----
39
------- test 7 ----
37
Keep up the good work!


Thanks for your submission!

 

Subset SumsRob Kolstad

This is a classic dynamic programming problem. Hal's solution is shown below.

/* Calculate how many two-way partitions of {1, 2, ..., N} are
   even splits (the sums of the elements of both partition are equal) */

#include <stdio.h>
#include <string.h>

#define MAXSUM 637

unsigned int numsets[637][51];

int max;
unsigned int sum;

main(int argc, char **argv)
{
  int lv, lv2, lv3;
  int cnt;
  FILE *fin, *fout;

  fin = fopen ("subset.in", "r");
  fscanf(fin, "%d", &max);
  fclose (fin);
  fout = fopen("subset.out", "w");

  if ((max % 4) == 1 || (max % 4) == 2) {
    fprintf (stderr, "0\n");
    exit(1);
  }

  sum = max * (max+1) / 4;

    memset(numsets, 0, sizeof(numsets[0]));
    numsets[0][0] = 1;
    for (lv = 1; lv < max; lv++) {
      for (lv2 = 0; lv2 <= sum; lv2++)
        numsets[lv2][lv] = numsets[lv2][lv-1];
      for (lv2 = 0; lv2 <= sum-lv; lv2++)
        numsets[lv2+lv][lv] += numsets[lv2][lv-1];
    }

    fprintf (fout, "%u\n", numsets[sum][max-1]);
    fclose (fout);
  exit (0);
}

and here's an even more concise solution from Nick Tomitov of Bulgaria:

#include <fstream>
using namespace std;
const unsigned int MAX_SUM = 1024;
int n;
unsigned long long int dyn[MAX_SUM];
ifstream fin ("subset.in");
ofstream fout ("subset.out");

int main() {
    fin >> n;
    fin.close();
    int s = n*(n+1);
    if (s % 4) {
        fout << 0 << endl;
        fout.close ();
        return ;
    }
    s /= 4;
    int i, j;
    dyn [0] = 1;
    for (i = 1; i <= n; i++)
        for (j = s; j >= i; j--) 
            dyn[j] += dyn[j-i];
    fout << (dyn[s]/2) << endl;
    fout.close();
    return 0;
}


 

USACO section 3.4 Closed Fences(计算几何+叉积+二分) Closed Fences A closed fence in the plane is a set of non-crossing, connected line segments with N corners (3 i, yi}, i in (1..N). Every pair of adjacent vertices defines a side of the fence. 阅读详情

相关推荐

Prostate_Cancer.csv

Prostate_Cancer.csv数据集,100条癌症数据,供实验用。

前列腺癌Gleason分级AI诊断:多尺度CNN与病理先验融合实战

Gleason分级是前列腺癌临床诊疗的金标准,其本质是对腺体形态学结构的精细判读,属于医学图像分析中典型的弱监督、小目标、高专业性任务。其技术难点在于影像分辨率与病理组织学尺度间的鸿沟,以及算法输出需满足临床可解释性与可追溯性要求。机器学习在该场景的价值,不在于替代医生,而在于将模糊的经验性描述(如‘腺体拥挤’‘基底细胞断裂’)转化为可量化、可验证的数值特征,并嵌入DICOM-SR等临床工作流标准。本文以Kaggle前列腺癌挑战赛冠军方案为蓝本,详解如何通过多尺度CNN架构、病理先验注意力门控(PIAG)与

weixin_30239339的博客 180

电子系统设计与创新实践作品:简易电子琴

题目 4:简易电子琴 (1)具有8个音阶按键,每个音阶按键频率如表1所示。 (2)每个按键对应LED显示。 (3)音阶信号经功放连接8欧姆扬声器,输出功率可达1W。 (4)每个音阶输出音量一样。 (5)扬声器输出音量可调节,音量调节分为高、中、低三档,并进行档位显示。

weixin_47510497的博客 2868

USACO-Section2.2 Subset Sums

USACO-Section2.2 Subset Sums

qq_34600424的博客 516

Simulink小白必看:用Simscape轻松搭建二阶倒立摆模型(附完整参数配置)

本文为Simulink初学者提供了使用Simscape Multibody模块库,从零开始搭建二阶倒立摆非线性物理模型的完整实战教程。文章详细讲解了如何通过物理组件(如刚体、关节)直观建模,替代复杂的数学推导,并涵盖参数配置、仿真调试及模型线性化方法,帮助读者快速掌握这一高效的物理建模路径。

grpc6streamer的博客 1054

使用QGIS提取天地图遥感影像中的建筑物轮廓,及AI方案

用QGIS的插件提取遥感影像中的建筑轮廓,并提供了几个AI提取建筑轮廓的方案。

cky_123的博客 2万+

USACO Section2.2 Subset Sums

Subset Sums JRM For many sets of consecutive integers from 1 through N (1 For example, if N=3, one can partition the set {1, 2, 3} in one way so that the sums of both subsets are identical: {

248

论文理解【LLM-Agent】——【SkillOpt】Executive Strategy for Self-Evolving Agent Skills

SkillOpt 将 agent skill 从人工或一次性 prompt artifact 转化为可验证、可控、可迁移的文本空间“可训练状态”,通过有界编辑、验证门控、拒绝编辑反馈和 slow/meta update,在不更新模型权重的情况下显著提升多类 agent 任务表现

佚失的诗篇 671

USACO section2.2 Subset Sums题解&代码

恩...连交三次才过...又被USACO的数据教做人... 首先这题是一道很简单的01背包...或者说递推... 只要O(n^3)就可以了... 然后...ll大法好... 最后...为什么我会忘记中间数可能不是整数这个事实...我也不知道... /* ID:rainbow16 LANG:C++ TASK:subset */ #include #include using namespa

Rainbow6174's home 734

使用CAPL实现DoIP刷写

本文介绍了使用CAPL实现基于DoIP协议的ECU刷写方法,包含完整源码。

qq_56033302的博客 2782

ATECC508A芯片开发笔记(三):获取508A串号、随机数源码及I2C抓包分析

ATECC508A芯片开发笔记(三):读取508A串号、获取随机数源码及I2C抓包分析 APP层函数编写及源码分析 At508_GetSernum() GetSernum抓包记录 At508_GetRandom( ) GetRandom抓包记录 经过前两章节,CryptoAuthlib库已经成功适配在代码中了,下面简单测试一下API函数,并分析下I2C通讯流程: APP层函数编写及源码分析利用li

HowieXue 薛永浩的博客 2万+

USACO-Section2.2 Subset Sums [动态规划]

题目大意 对于从1到N (1 <= N <= 39) 的连续整数集合,能划分成两个子集合,且保证每个集合的数字和是相等的。举个例子,如果N=3,对于{1,2,3}能划分成两个子集合,每个子集合的所有数字和是相等的: {3} 和 {1,2}这是唯一一种分法(交换集合位置被认为是同一种划分方案,因此不会增加划分方案总数) 如果N=7,有四种方法能划分集合{1,2,3,4,5,6,7},每一种分法的子

yoer77的博客 466

【WRF模拟】土地利用类型的确定:最大覆盖法/瓦片法

土地利用类型的确定:最大覆盖法/瓦片法

WW、forever的博客 1539

USACO Section2.2 Subset Sums 解题报告 【icedream61】

    subset解题报告------------------------------------------------------------------------------------------------------------------------------------------------【题目】   把1~N分成两组,让他们的和相等,请问这样的分组有多少种?   但顺...

weixin_30487317的博客 157

瑞芯微RK3588/RK3576平台采集BT1120(fpga)驱动调试记录

本文介绍了在RK3576/RK3588平台上采集FPGA传输的BT1120数据的方法。主要内容包括:1)硬件连接需确认管脚复用和图像格式(通常为uyvy_2x8);2)设备树配置要点,如I2C总线设置、BT1120模式指定等;3)驱动适配注意事项,包括时钟极性、分辨率格式等关键参数;4)测试方法,通过media-ctl和v4l2-ctl工具验证数据采集。文中特别指出RK3576和RK3588配置的通用性,并提供了常见问题的排查思路。

bingdund的博客 937

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

USACO section 3.1 Contact(AC自动机)

Contact IOI'98 The cows have developed a new interest in scanning the universe outside their farm with radiotelescopes. Recently, they noticed a very curious microwave pulsing emission sent right f

nealgavin的专栏 9384

最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版

Part-E1-SDIO-Specification-Ver4.00-Final-120220.pdf:最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版,包括SDIO 接口的外设,SDIO 接口官方协议等。

USCAO section 1.3 Calf Flac

Calf Flac It is said that if you give an infinite number of cows an infinite number of heavy-duty laptops (with very large keys), that they will ultimately produce all the world's great palindromes

nealgavin的专栏 6656

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

USACO section 1.5 Checker Challenge(dfs深搜)

Checker ChallengeExamine the 6x6 checkerboard below and note that the six checkers are arranged on the board so that one and only one is placed in each row and each column, and there is never more tha

nealgavin的专栏 1924

USACO section 2.3 Longest Prefix(dp)

Longest Prefix IOI'96 The structure of some biological objects is represented by the sequence of their constituents, where each part is denote by an uppercase letter. Biologists are interested in

nealgavin的专栏 1487

USACO section 2.4 Cow Tours(并查集+最短路)

Cow Tours Farmer John has a number of pastures on his farm. Cow paths connect some pastures with certain other pastures, forming a field. But, at the present time, you can find at least two pasture

nealgavin的专栏 1406

USACO section3.1 Shaping Regions(漂浮法)

Shaping Regions N opaque rectangles (1 <= N <= 1000) of various colors are placed on a white sheet of paper whose size is A wide by B long. The rectangles are put with their sides parallel to the s

nealgavin的专栏 1052

USACO section 2.2 Runaround Numbers

Runaround Numbers Runaround numbers are integers with unique digits, none of which is zero (e.g., 81362) that also have an interesting property, exemplified by this demonstration: If you start

nealgavin的专栏 991

USACO section 3.1 Stamps(dp)

Stamps Given a set of N stamp values (e.g., {1 cent, 3 cents}) and an upper limit K to the number of stamps that can fit on an envelope, calculate the largest unbroken list of postages from 1 cent

nealgavin的专栏 980

USCAO section 4.1 Fence Loops(最短路,最小环,5级)

Fence Loops The fences that surround Farmer Brown's collection of pastures have gotten out of control. They are made up of straight segments from 1 through 200 feet long that join together only a

nealgavin的专栏 941

USACO section 1.5 Superprime Rib(深搜)

Superprime RibButchering Farmer John's cows always yields the best prime rib. You can tell prime ribs by looking at the digits lovingly stamped across them, one by one, by FJ and the USDA. Farmer John

nealgavin的专栏 938

USACO section 2.2 Party Lamps

Party Lamps IOI 98 To brighten up the gala dinner of the IOI'98 we have a set of N (10 <= N <= 100) colored lamps numbered from 1 to N. The lamps are connected to four buttons: Button 1: Wh

nealgavin的专栏 914
上一篇: USCAO section 2,2 Preface Numbering(算是DP吧)
下一篇: USACO section 2.2 Runaround Numbers
剑不飞
博客等级 码龄15年 86粉丝 505原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址