Hãy nhập câu hỏi của bạn vào đây, nếu là tài khoản VIP, bạn sẽ được ưu tiên trả lời.
#include <iostream>
#include <vector>
using namespace std;
vector<int> primeFactors(int n) {
vector<int> factors;
for (int i = 2; i * i <= n; i++) {
while (n % i == 0) {
factors.push_back(i);
n /= i;
}
}
if (n > 1) factors.push_back(n);
return factors;
}
int main() {
int n, k;
cin >> n >> k;
vector<int> a(n);
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
vector<int> factors = primeFactors(k);
int sum = accumulate(a.begin(), a.end(), 0);
vector<vector<bool>> dp(n+1, vector<bool>(sum+1, false));
dp[0][0] = true;
for (int i = 1; i <= n; ++i) {
for (int j = 0; j <= sum; ++j) {
dp[i][j] = dp[i-1][j];
if (j >= a[i-1]) {
for (int factor : factors) {
if (a[i-1] % factor == 0) {
dp[i][j] = dp[i][j] || dp[i-1][j-a[i-1]];
break;
}
}
}
}
}
for (int j = 0; j <= sum; ++j) {
if (dp[n][j]) {
cout << j << endl;
break;
}
}
return 0;
}
const fi='xsum1.inp';
fo='xsum1.out';
var f1,f2:text;
a:array[1..20000]of integer;
i,n,j,x,dem,k,m,t:integer;
begin
assign(f1,fi); reset(f1);
assign(f2,fo); rewrite(f2);
readln(f1,n,x);
for i:=1 to n do
read(f1,a[i]);
dem:=0;
for i:=1 to n do
for j:=1 to n do
begin
for k:=1 to n do
if (i<j) and (j<k) then
begin
t:=0;
for m:=i to k do
t:=t+a[m];
if t=x then inc(dem);
end;
end;
for i:=1 to n do
if x=a[i] then inc(dem);
writeln(f2,dem);
close(f1);
close(f2);
end.
program COST;
var n,m,s,dem:int64;a:
array[1..1000000] of int64;
i,j:longint;
begin
readln(n,m);
for i := 1 to n do read(a[i]);
dem := 0;
for i := 1 to n do
begin
s := 0;
for j := i to n do
begin
s := s+a[j];
if s <= m then
begin
inc(dem);
continue;
end
else if a[j] > m then break;
end;
end;
write(dem);
end.
uses crt;
var a:array[1..100]of integer;
i,n,t,min,vt:integer;
begin
clrscr;
write('Nhap n='); readln(n);
for i:=1 to n do
begin
write('A[',i,']='); readln(a[i]);
end;
t:=0;
for i:=1 to n do
if a[i] mod 3=0 then t:=t+a[i];
writeln('Tong cac so chia het cho 3 la: ',t);
min:=a[1];
for i:=1 to n do
if min>a[i] then min:=a[i];
vt:=1;
for i:=1 to n do
if min=a[i] then
begin
if vt<i then vt:=i;
end;
writeln('Gia tri nho nhat la: ',min);
writeln('Vi tri la: ',vt);
readln;
end.