3.8w+
社区成员
T1
//01背包
#include <iostream>
using namespace std;
int f[100005];
int main()
{
int n,V;
cin>>V>>n;
int v;
for(int i=1; i<=n; i++)
{
cin>>v;
for(int j=V; j>=v; j--)
if(f[j]<f[j-v]+v)
f[j]=f[j-v]+v;
}
cout<<V-f[V];
return 0;
}
T2
//二分答案(至少,往左找)
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
const int N=1e5+100;
int h[N],w[N];
int n,k;
bool check(int x)
{
int res=0;
for(int i=0; i<n; i++)
{
res+=(LL)(h[i]/x)*(w[i]/x);
}
if(res>=k) return true;
else return false;
}
int main()
{
scanf("%d%d",&n,&k);
for(int i=0; i<n; i++) scanf("%d%d",&h[i],&w[i]);
int l=0, r=1e5+10;
while(l<r)
{
int mid=(l+r+1)>>1;
if(check(mid)) l=mid;
else r=mid-1;
}
cout<<l;
return 0;
}
T3
//二分(至多,往右找)
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N=1e5+10;
int a[N];
int n,c,maxn;
bool check(int x)
{
int y=a[0], res=1;
for(int i=1; i<n; i++)
{
if(a[i]-y>=x) res++, y=a[i];
}
return res>=c;
}
int main()
{
cin>>n>>c;
for(int i=0; i<n; i++)
{
cin>>a[i];
maxn=max(maxn,a[i]);
}
sort(a,a+n);
int l=0, r=maxn;
while(l<r)
{
int mid=(l+r+1)/2;
if(check(mid)) l=mid;
else r=mid-1;
}
cout<<l;
return 0;
}
T4
//dfs暴搜
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define MOD 1000000009
using namespace std;
typedef long long LL;
LL s,n,k;
LL a[30];
bool is_prime(int x) {
for(int i=2; i<=sqrt(x); i++)
if(x%i==0) return false;
return true;
}
void dfs(int sum,int from,int cur) {
if(cur==k) {
if(is_prime(sum)) s++;
return;
}
for(int i=from; i<n; i++)
dfs(sum+a[i],i+1,cur+1);
}
int main() {
cin>>n>>k;
for(int i=0; i<n; i++)
cin>>a[i];
dfs(0,0,0);
cout<<s;
return 0;
}