Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <algorithm>
- #include <numeric>
- const int N = 1e3;
- using namespace std;
- int a[N+1], dp[N+1][51], n, k, sum = 0;
- void read()
- {
- cin >> n >> k;
- for (int i = 1; i <= n; ++ i)
- {
- cin >> a[i];
- a[i] = a[i] % k;
- sum = (sum + a[i])%k;
- }
- }
- void debug()
- {
- for (int i = 0; i <= n; ++ i)
- {
- for (int j = 0; j < k; ++ j)
- {
- if (dp[i][j] >= 1e9)
- {
- cout << -1;
- }
- else
- {
- cout << dp[i][j];
- }
- cout << ' ';
- }
- cout <<a[i] << '\n';
- }
- cout << sum;
- }
- void traceback()
- {
- cout << n - dp[n][sum] << '\n';
- for (int i = n; i >= 1; -- i)
- {
- if (dp[i-1][sum] == dp[i][sum])
- {
- cout << i <<' ';
- }
- else
- {
- sum = (sum - a[i] + k)%k;
- }
- }
- }
- void solve()
- {
- dp[0][0] = 0;
- for (int i = 1; i < k; ++ i)
- {
- dp[0][i] = 1e9;
- }
- for (int i = 1; i <= n; ++ i)
- {
- for (int j = 0; j < k; ++ j)
- {
- dp[i][j] = min(dp[i-1][j], dp[i-1][(j - a[i] + k) % k] + 1);
- }
- }
- //debug();
- traceback();
- }
- int main()
- {
- ios_base::sync_with_stdio(false);
- cin.tie(nullptr);
- read();
- solve();
- }
- // Chuyển đề sang tìm số số nhỏ nhất sao cho tổng lại % k = sum % k
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement