UVA_10336_Divisibility

#include<iostream>    
#include<sstream>    
#include<string>    
#include<vector>    
#include<list>    
#include<set>    
#include<map>    
#include<stack>    
#include<queue>    
#include<algorithm>  
#include<bitset>
#pragma warning(disable:4996)    
using std::cin;
using std::cout;
using std::endl;
using std::stringstream;
using std::string;
using std::vector;
using std::list;
using std::pair;
using std::set;
using std::multiset;
using std::map;
using std::multimap;
using std::stack;
using std::queue;
using std::priority_queue;
using std::bitset;
int main()
{
	//freopen("input.txt", "r", stdin);    
	//freopen("output.txt", "w", stdout); 
	int T;
	while (cin >> T)
	{
		while (T--)
		{
			int n, divisor; cin >> n >> divisor;
			vector<int>value(n);
			for (int i = 0; i < n; i++)
			{
				cin >> value[i];
				value[i] = (int)fabs(value[i]) % divisor;
			}
			vector<vector<bool>>dp(n, (vector<bool>)divisor);
			dp[0][value[0]] = true;
			for (int i = 1; i < n; i++)
			{
				for (int j = 0; j < divisor; j++)
				{
					if (dp[i - 1][j])
					{
						dp[i][(divisor + value[i] + j) % divisor] = true;
						dp[i][(divisor - value[i] + j) % divisor] = true;
					}
				}
			}
			if (dp[n - 1][0])
			{
				cout << "Divisible" << endl;
			}
			else
			{
				cout << "Not divisible" << endl;
			}
		}
	}
	return 0;
}

你可能感兴趣的:(dynamic,programming,uva)