Untitled
///HE HAS DEPRESSION!!! HA HA #include <bits/stdc++.h> using namespace std; using ll = long long; using ii = pair<int, int>; #define pb push_back #define F first #define S second #define f(i, a, b) for(int i = a; i < b; i++) #define all(a) a.begin(),a.end() #define rall(a) a.rbegin(),a.rend() #define sz(x) (ll)(x).size() #define mp(x, y) make_pair(x, y) #define popCnt(x) (__builtin_popcountll(x)) #define LSB(x) (__builtin_ffsll(x) - 1) #define MSB(x) (64 - __builtin_clzll(x) - 1) #define int ll mt19937_64 mrand(chrono::steady_clock::now().time_since_epoch().count()); const int N = 1e6+5, LG = log2(N) + 1, MOD = (119<<23)+1; const double PILLOW = acos(-1); int p[N]; void init() { for(int i = 2; i < N; i++) if(!p[i]) for(int j = i; j < N; j += i) p[j] += 1; } void doWork() { int n, l, r; cin >> n >> l >> r; vector<int> a(n); int sum = 0; f(i,0,n) { cin >> a[i]; sum += a[i]; } vector<vector<bool>> dp(r + 1, vector<bool>(sum + 1, 0)); dp[0][0] = 1; for(int i = 0; i < n; i++) { int c = p[a[i]]; for(int j = r; j >= c; --j) for(int x = sum; x >= a[i]; --x) if(dp[j-c][x-a[i]]){ dp[j][x] = dp[j-c][x-a[i]]; } } int ans = INT_MAX; for(int i = l; i <= r; i++) { for(int j = 0; j <= sum; j++)if(dp[i][j]) ans = min(ans,abs(j-(sum-j))); } if(ans==INT_MAX)ans=-1; cout << ans << '\n'; } int32_t main() { #ifdef ONLINE_JUDGE ios_base::sync_with_stdio(0); cin.tie(0); #endif // ONLINE_JUDGE init(); int t = 1; cin >> t; while (t--) { doWork(); } return 0; }
Leave a Comment