├── README.md ├── .gitignore ├── leetCode ├── distinct-subsequences.cpp ├── super-ugly-number.cpp ├── ugly-number-ii.cpp ├── house-robber.cpp ├── decode-ways.cpp ├── dungeon-game.cpp ├── burst-balloons.cpp ├── house-robber-ii.cpp ├── word-break.cpp ├── range-sum-query-2d-immutable.cpp ├── number-of-subsequences-that-satisfy-the-given-sum-condition.cpp ├── regular-expression-matching.cpp ├── max-value-of-equation.cpp ├── longest-increasing-path-in-a-matrix.cpp ├── palindrome-partitioning-ii.cpp ├── scramble-string.cpp ├── palindrome-partitioning.cpp └── word-break-ii.cpp ├── spoj ├── FUNPROB.cpp ├── AGGRCOW.cpp ├── ELIS.cpp ├── TRT.cpp ├── PT07Y.cpp ├── ONP.cpp ├── XOINC.cpp ├── WEIRDFN.cpp ├── TWINSNOW.cpp ├── BYTESM2.cpp ├── BLOPER.cpp ├── CHOCOLA.cpp ├── MAKETREE.cpp ├── PT07Z.cpp ├── PERMUT1.cpp └── INVCNT.cpp ├── UVa ├── 100 │ ├── 10036.cpp │ ├── 10003.cpp │ ├── 10056.cpp │ └── 10004.cpp ├── 101 │ ├── 10115.cpp │ ├── 10192.cpp │ └── 10130.cpp ├── 102 │ ├── 10264.cpp │ └── 10242.cpp ├── 103 │ ├── 10302.cpp │ ├── 10340.cpp │ ├── 10325.cpp │ ├── 10344.cpp │ ├── 10368.cpp │ ├── 10394.cpp │ └── 10305.cpp ├── 104 │ ├── 10469.cpp │ ├── 10491.cpp │ └── 10474.cpp ├── 105 │ └── 10509.cpp ├── 106 │ ├── 10699.cpp │ ├── 10617.cpp │ ├── 10684.cpp │ ├── 10626.cpp │ ├── 10651.cpp │ └── 10611.cpp ├── 107 │ ├── 10790.cpp │ ├── 10703.cpp │ └── 10739.cpp ├── 108 │ ├── 10812.cpp │ ├── 10815.cpp │ ├── 10843.cpp │ ├── 10892.cpp │ └── 10865.cpp ├── 109 │ ├── 10976.cpp │ ├── 10945.cpp │ ├── 10954.cpp │ └── 10935.cpp ├── 110 │ ├── 11053.cpp │ ├── 11026.cpp │ └── 11062.cpp ├── 112 │ ├── 11207.cpp │ └── 11286.cpp ├── 113 │ ├── 11345.cpp │ ├── 11388.cpp │ └── 11341.cpp ├── 114 │ ├── 11462.cpp │ ├── 11417.cpp │ ├── 11420.cpp │ ├── 11495.cpp │ └── 11413.cpp ├── 115 │ ├── 11518.cpp │ ├── 11520.cpp │ └── 11536.cpp ├── 116 │ ├── 11679.cpp │ └── 11661.cpp ├── 117 │ ├── 11703.cpp │ ├── 11730.cpp │ └── 11753.cpp ├── 118 │ └── 11849.cpp ├── 119 │ ├── 11900.cpp │ ├── 11933.cpp │ ├── 11988.cpp │ ├── 11991.cpp │ └── 11995.cpp ├── 120 │ ├── 12095.cpp │ ├── 12032.cpp │ └── 12034.cpp ├── 121 │ └── 12124.cpp ├── 122 │ └── 12289.cpp ├── 123 │ ├── 12318.cpp │ └── 12346.cpp ├── 124 │ ├── 12461.cpp │ ├── 12405.cpp │ └── 12484.cpp ├── 126 │ └── 12626.cpp ├── 127 │ └── 12748.cpp ├── 128 │ └── 12869.cpp ├── 129 │ └── 12952.cpp ├── 003 │ ├── 374.cpp │ ├── 382.cpp │ ├── 356.cpp │ └── 389.cpp ├── 002 │ ├── 264.cpp │ ├── 294.cpp │ └── 229.cpp ├── 004 │ ├── 408.cpp │ ├── 494.cpp │ ├── 483.cpp │ ├── 414.cpp │ ├── 460.cpp │ ├── 417.cpp │ └── 498.cpp ├── 012 │ ├── 1203.cpp │ ├── 1230.cpp │ ├── 1260.cpp │ └── 1209.cpp ├── 011 │ ├── 1121.cpp │ └── 1112.cpp ├── 005 │ ├── 541.cpp │ ├── 514.cpp │ ├── 562.cpp │ └── 574.cpp ├── 001 │ ├── 128.cpp │ ├── 127.cpp │ └── 193.cpp ├── 013 │ ├── 1346.cpp │ └── 1362.cpp ├── 006 │ ├── 616.cpp │ ├── 668.cpp │ └── 624.cpp └── 015 │ └── 1555.cpp ├── TIMUS ├── 1638.cpp ├── 1263.cpp ├── 1350.cpp ├── 1017.cpp └── 1525.cpp ├── TopCoder ├── BestView.cpp └── UnsealTheSafe.cpp ├── DPproblems ├── 41-FibonacciSeries.cpp ├── 20-MaximumProductCutting.cpp ├── 37-CountAs.cpp ├── 35-UglyNumbers.cpp ├── 27-NumberOfWaysToScorePoints.cpp ├── 43-BoxStacking.cpp ├── 11-EggDropping.cpp ├── 9-DiceThrowWays.cpp ├── 40-MinimumCostTrainTicket.cpp ├── 32-SubsetSum.cpp └── 5-CountNumberOfBinaryWithoutConsecutive1s.cpp ├── LiveArchive └── 8078.cpp └── newcomers ├── difference between subarray and subsequence.md └── basics math algorithms.cpp /README.md: -------------------------------------------------------------------------------- 1 | # competitiveProgramming 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | TopCoder/.project 3 | problems.code-workspace 4 | TopCoder/a.exe 5 | -------------------------------------------------------------------------------- /leetCode/distinct-subsequences.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numDistinct(string s, string t) { 4 | int n = s.size(), m = t.size(); 5 | vector dp(m + 1); 6 | dp[m] = 1; 7 | for (int i = n - 1; i >= 0; i--) 8 | for (int j = 0; j < m; j++) 9 | if (s[i] == t[j]) dp[j] += dp[j + 1]; 10 | return dp[0]; 11 | } 12 | }; 13 | -------------------------------------------------------------------------------- /leetCode/super-ugly-number.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int nthSuperUglyNumber(int n, vector& primes) { 4 | set st; 5 | st.insert(1); 6 | long long ugly = 0; 7 | while (n--) { 8 | ugly = *st.begin(); st.erase(st.begin()); 9 | for (auto it : primes) 10 | st.insert(ugly * it); 11 | } 12 | return ugly; 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /leetCode/ugly-number-ii.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int nthUglyNumber(int n) { 4 | vector primes = { 2,3,5 }; 5 | set st; 6 | st.insert(1); 7 | long long last = 1; 8 | while (n--) { 9 | last = *st.begin(); st.erase(st.begin()); 10 | for (auto it : primes) 11 | st.insert(last * it); 12 | } 13 | return last; 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /leetCode/house-robber.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | vector v, mem; 3 | int maxSum(int index) { 4 | if (index >= v.size())return 0; 5 | int& rt = mem[index]; 6 | if (~rt)return rt; 7 | rt = maxSum(index + 1); 8 | rt = max(rt, v[index] + maxSum(index + 2)); 9 | return rt; 10 | } 11 | public: 12 | int rob(vector& nums) { 13 | v = nums; 14 | mem = vector(nums.size(), -1); 15 | return maxSum(0); 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /leetCode/decode-ways.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numDecodings(string s) { 4 | int n = s.size(); 5 | vector dp(n + 1); 6 | dp[n] = 1; 7 | for (int i = n - 1; i >= 0; i--) { 8 | if (s[i] == '0')continue; 9 | dp[i] += dp[i + 1]; 10 | if (s[i] == '1' && i + 1 < n) 11 | dp[i] += dp[i + 2]; 12 | else if (s[i] == '2' && i + 1 < n && s[i + 1] <= '6') 13 | dp[i] += dp[i + 2]; 14 | } 15 | return dp[0]; 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /leetCode/dungeon-game.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> v, mem, vis; 4 | int n, m; 5 | int solve(int r, int c) { 6 | if (r >= n || c >= m) return -1e9; 7 | if (r == n - 1 && c == m - 1) return min(0, v[r][c]); 8 | int& rt = mem[r][c]; 9 | if (vis[r][c])return rt; 10 | vis[r][c] = 1; 11 | rt = max(solve(r + 1, c), solve(r, c + 1)) + v[r][c]; 12 | return rt = min(0, rt); 13 | } 14 | int calculateMinimumHP(vector>& dungeon) { 15 | v = dungeon; 16 | n = v.size(); m = v[0].size(); 17 | vis = mem = vector>(n + 1, vector(m + 1)); 18 | return max(1, 1- solve(0, 0)); 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /leetCode/burst-balloons.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector v; 4 | vector> mem; 5 | int solve(int left, int right) { 6 | int& rt = mem[left][right]; 7 | if (~rt)return rt; 8 | rt = 0; 9 | for (int i = left + 1; i < right; i++) { 10 | int cur = v[i] * v[left] * v[right] + solve(left, i) + solve(i, right); 11 | rt = max(rt, cur); 12 | } 13 | return rt; 14 | } 15 | int maxCoins(vector& nums) { 16 | v.push_back(1); 17 | v.insert(v.end(), nums.begin(), nums.end()); 18 | v.push_back(1); 19 | mem = vector>(v.size(), vector(v.size(), -1)); 20 | return solve(0, v.size() - 1); 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /leetCode/house-robber-ii.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | vector v; 3 | vector> mem; 4 | int maxSumCycle(int index, bool takeFirst) { 5 | if (index >= v.size())return 0; 6 | int& rt = mem[index][takeFirst]; 7 | if (~rt)return rt; 8 | rt = maxSumCycle(index + 1, takeFirst); 9 | if (index + 1 != v.size() || !takeFirst) 10 | rt = max(rt, v[index] + maxSumCycle(index + 2, takeFirst)); 11 | return rt; 12 | } 13 | public: 14 | int rob(vector& nums) { 15 | if (nums.empty())return 0; 16 | v = nums; 17 | mem = vector>(nums.size(), { -1,-1 }); 18 | return max(maxSumCycle(1, 0), maxSumCycle(2, 1) + v[0]); 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /spoj/FUNPROB.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | #define endl "\n" 6 | void run() { 7 | ios::sync_with_stdio(false); 8 | cin.tie(NULL); 9 | cout.tie(NULL); 10 | #ifndef ONLINE_JUDGE 11 | freopen("input.in", "r", stdin); 12 | //freopen("output.out", "w", stdout); 13 | #else 14 | #endif 15 | } 16 | 17 | int main() { 18 | run(); 19 | cout << fixed << setprecision(6); 20 | int n, m; 21 | while (cin >> n >> m) { 22 | if (n == m && n == 0) 23 | return 0; 24 | if (n > m) 25 | cout << 0.0 << endl; 26 | else 27 | cout << (double) (m + 1 - n) / (m + 1) << endl; 28 | 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /spoj/AGGRCOW.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int n, c; 5 | vector v; 6 | bool ok(int dis) { 7 | int last = v[0], cnt = 1; 8 | for (int i = 1; i < n; i++) { 9 | if (v[i] - last >= dis) { 10 | last = v[i]; 11 | cnt++; 12 | } 13 | } 14 | return cnt >= c; 15 | } 16 | 17 | int main() { 18 | int t; cin >> t; 19 | while (t--) { 20 | cin >> n >> c; 21 | v = vector(n); 22 | for (int i = 0; i < n; i++) 23 | cin >> v[i]; 24 | sort(v.begin(), v.end()); 25 | int st = 1, ed = 1e9, md, ans = -1; 26 | while (st <= ed) { 27 | md = st + ed >> 1; 28 | if (ok(md)) 29 | st = md + 1, ans = md; 30 | else ed = md - 1; 31 | } 32 | cout << ans << endl; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /leetCode/word-break.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | set dictionary; 4 | vector words; 5 | string word; 6 | vector mem; 7 | int canSplit(int start) { 8 | if (start == word.size())return 1; 9 | int& rt = mem[start]; 10 | if (~rt)return rt; 11 | rt = 0; 12 | string subWord; 13 | for (int end = start; end < word.size(); end++) { 14 | subWord += word[end]; 15 | if (dictionary.find(subWord) != dictionary.end()) 16 | rt |= canSplit(end + 1); 17 | } 18 | return rt; 19 | } 20 | 21 | bool wordBreak(string s, vector& wordDict) { 22 | word = s; 23 | dictionary = set(wordDict.begin(), wordDict.end()); 24 | mem = vector(word.size() + 1, -1); 25 | return canSplit(0); 26 | } 27 | }; 28 | -------------------------------------------------------------------------------- /UVa/124/12461.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | #define endl "\n" 6 | #define ll long long 7 | #define oo 0x3f3f3f3fLL 8 | #define sz(s) (int)(s.size()) 9 | #define RT(s) return cout<> a, a) { 28 | cout << "1/2\n"; 29 | } 30 | } -------------------------------------------------------------------------------- /UVa/103/10302.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | #define endl "\n" 6 | #define ll long long 7 | #define oo 0x3f3f3f3fLL 8 | #define sz(s) (int)(s.size()) 9 | #define RT(s) return cout<> x) { 28 | cout << x * x * (x + 1) * (x + 1) / 4 << endl; 29 | } 30 | } -------------------------------------------------------------------------------- /UVa/129/12952.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | #define endl "\n" 6 | #define ll long long 7 | #define oo 0x3f3f3f3fLL 8 | #define sz(s) (int)(s.size()) 9 | #define RT(s) return cout<> a >> b) { 28 | if (a == b) 29 | cout << a << endl; 30 | else cout << max(a, b) << endl; 31 | } 32 | } -------------------------------------------------------------------------------- /leetCode/range-sum-query-2d-immutable.cpp: -------------------------------------------------------------------------------- 1 | class NumMatrix { 2 | public: 3 | vector> sum; 4 | NumMatrix(vector>& matrix) { 5 | sum = matrix; 6 | for (int i = 0; i < matrix.size(); i++) 7 | for (int j = 0; j < matrix[i].size(); j++) { 8 | if (i) sum[i][j] += sum[i - 1][j]; 9 | if (j) sum[i][j] += sum[i][j - 1]; 10 | if (i && j) sum[i][j] -= sum[i - 1][j - 1]; 11 | } 12 | } 13 | 14 | int sumRegion(int row1, int col1, int row2, int col2) { 15 | int rt = sum[row2][col2]; 16 | if (row1)rt -= sum[row1 - 1][col2]; 17 | if (col1) rt -= sum[row2][col1 - 1]; 18 | if (row1 && col1) rt += sum[row1 - 1][col1 - 1]; 19 | return rt; 20 | } 21 | }; 22 | 23 | /** 24 | * Your NumMatrix object will be instantiated and called as such: 25 | * NumMatrix* obj = new NumMatrix(matrix); 26 | * int param_1 = obj->sumRegion(row1,col1,row2,col2); 27 | */ -------------------------------------------------------------------------------- /leetCode/number-of-subsequences-that-satisfy-the-given-sum-condition.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | const int mod = 1e9+7; 3 | using ll = long long; 4 | ll power(ll x,ll y){ 5 | if(y == 0) 6 | return 1; 7 | if(y == 1) 8 | return x; 9 | ll r = power(x,y>>1); 10 | return ((r*r)%mod * power(x,y&1))%mod; 11 | } 12 | public: 13 | int numSubseq(vector nums, int target) { 14 | sort(nums.begin(),nums.end()); 15 | int l = 0,r = 0; 16 | int n = nums.size(); 17 | int ans = 0; 18 | while(l> dp(sz(x) + 1, vector(sz(y) + 1)); 11 | dp[0][0] = 1; 12 | for (int i = 0; i <= sz(x); i++) 13 | for (int j = 0; j < sz(y); j++) { 14 | if (dp[i][j] == 0) continue; 15 | if (valid(i, j)) dp[i + 1][j + 1] |= dp[i][j]; 16 | if (j + 1 < sz(y) && y[j + 1] == '*') { 17 | dp[i][j + 2] |= dp[i][j]; 18 | if (valid(i, j)) 19 | dp[i + 1][j] |= dp[i][j]; 20 | } 21 | if (y[j] == '*') { 22 | dp[i][j + 1] |= dp[i][j]; 23 | if (valid(i, j - 1)) 24 | dp[i + 1][j] |= dp[i][j]; 25 | } 26 | } 27 | return dp[sz(x)][sz(y)]; 28 | } 29 | }; 30 | -------------------------------------------------------------------------------- /leetCode/max-value-of-equation.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | struct monoqueue { 3 | deque> q; 4 | void push(int val,int idx){ 5 | while(q.size() && q.back().first <= val) 6 | q.pop_back(); 7 | q.push_back({val,idx}); 8 | } 9 | void pop(int idx){ 10 | while(q.size() && q.front().second <= idx) 11 | q.pop_front(); 12 | } 13 | int getMax(){ 14 | return q.front().first; 15 | } 16 | }; 17 | public: 18 | int findMaxValueOfEquation(vector>& points, int k) { 19 | monoqueue q; 20 | int mx = -1e9; 21 | for(auto & it : points){ 22 | int x = it[0],y = it[1]; 23 | q.pop(x - k - 1); 24 | mx = max(mx,q.getMax() + y + x); 25 | q.push(y - x,x); 26 | } 27 | return mx; 28 | } 29 | }; -------------------------------------------------------------------------------- /UVa/103/10340.cpp: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | #include 4 | using namespace std; 5 | typedef long long ll; 6 | typedef unsigned long long ull; 7 | typedef long double ld; 8 | typedef vector vi; 9 | #define Fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); 10 | #define endl '\n' 11 | #define sz(s) (int)(s.size()) 12 | #define all(s) s.begin(),s.end() 13 | #define allr(s) s.rbegin(),s.rend() 14 | #define RT(s,v) return cout<> s >> t) { 26 | int i = 0, j = 0; 27 | while (i < sz(s) && j < sz(t)) { 28 | if (s[i] == t[j])i++; 29 | j++; 30 | } 31 | cout << ((i == sz(s)) ? "Yes" : "No") << endl; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /leetCode/longest-increasing-path-in-a-matrix.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> v, mem; 4 | vector dr, dc; 5 | bool valid(int i, int j) { 6 | return 0 <= i && i < v.size() && 0 <= j && j < v[i].size(); 7 | } 8 | int solve(int i, int j) { 9 | int& rt = mem[i][j]; 10 | if (~rt)return rt; 11 | rt = 1; 12 | for (int k = 0; k < 4; k++) { 13 | int x = i + dr[k], y = j + dc[k]; 14 | if (valid(x, y) && v[i][j] < v[x][y]) 15 | rt = max(rt, 1 + solve(x, y)); 16 | } 17 | return rt; 18 | } 19 | int longestIncreasingPath(vector>& matrix) { 20 | if (matrix.empty())return 0; 21 | dr = { 1,-1,0,0 }; 22 | dc = { 0,0,1,-1 }; 23 | int rt = 0; 24 | v = matrix; 25 | int n = v.size(), m = v[0].size(); 26 | mem = vector>(n, vector(m, -1)); 27 | for (int i = 0; i < n; i++)for (int j = 0; j < m; j++) 28 | rt = max(rt, solve(i, j)); 29 | return rt; 30 | } 31 | }; 32 | -------------------------------------------------------------------------------- /UVa/114/11462.cpp: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | #include 4 | using namespace std; 5 | typedef long long ll; 6 | typedef unsigned long long ull; 7 | typedef long double ld; 8 | typedef vector vi; 9 | #define Fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); 10 | #define endl '\n' 11 | #define sz(s) (int)(s.size()) 12 | #define all(s) s.begin(),s.end() 13 | #define allr(s) s.rbegin(),s.rend() 14 | #define RT(s) return cout<> n, n) { 26 | vector v(n); 27 | while (n--) cin >> v[n]; 28 | sort(all(v)); 29 | cout << v[0]; 30 | for (int i = 1; i < sz(v); i++) 31 | cout << ' ' << v[i]; 32 | cout << endl; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /TIMUS/1638.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | #define ll long long 6 | #define endl '\n' 7 | #define RT(v) return cout<> n >> m >> s >> e; 28 | if (s < e) cout << n * (e - s - 1) + m * 2 * (e - s) << endl; 29 | else { 30 | swap(s, e); 31 | cout << n * (e - s + 1) + m * 2 * (e - s) << endl; 32 | } 33 | } -------------------------------------------------------------------------------- /UVa/107/10790.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | #define endl "\n" 6 | #define ll long long 7 | #define oo 0x3f3f3f3fLL 8 | #define sz(s) (int)(s.size()) 9 | #define RT(s) return cout<> a >> b, a || b) { 35 | cout << "Case " << I++ << ": " << nC2(a) * nC2(b) << endl; 36 | } 37 | } -------------------------------------------------------------------------------- /UVa/114/11417.cpp: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | #include 4 | using namespace std; 5 | typedef long long ll; 6 | typedef unsigned long long ull; 7 | typedef long double ld; 8 | typedef vector vi; 9 | #define Fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); 10 | #define endl '\n' 11 | #define sz(s) (int)(s.size()) 12 | #define all(s) s.begin(),s.end() 13 | #define allr(s) s.rbegin(),s.rend() 14 | #define RT(s) return cout<> n,n) { 27 | ll g = 0; 28 | for (int i = 1; i < n; i++) 29 | for (int j = i + 1; j <= n; j++) 30 | g += gcd(i, j); 31 | cout << g << endl; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /TIMUS/1263.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | #define ll long long 5 | #define endl '\n' 6 | #define RT(v) return cout<> n >> m; 27 | vector cnt(n + 1); 28 | for (int i = 0; i < m; i++) { 29 | int x; cin >> x; 30 | cnt[x]++; 31 | } 32 | cout << fixed << setprecision(2); 33 | for (int i = 1; i <= n; i++) { 34 | cout << cnt[i] * 100.0 / m << "%" << endl; 35 | } 36 | } -------------------------------------------------------------------------------- /UVa/108/10812.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | #define endl "\n" 6 | #define ll long long 7 | #define oo 0x3f3f3f3fLL 8 | #define sz(s) (int)(s.size()) 9 | #define RT(s) return cout<> t; 27 | while (t--) { 28 | int sum, dif; 29 | cin >> sum >> dif; 30 | int a = sum + dif >> 1; 31 | int b = sum - a; 32 | if (b < 0 || abs(a - b) != dif) 33 | cout << "impossible\n"; 34 | else cout << a << ' ' << b << endl; 35 | } 36 | } -------------------------------------------------------------------------------- /UVa/124/12405.cpp: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | #include 4 | using namespace std; 5 | typedef long long ll; 6 | typedef unsigned long long ull; 7 | typedef long double ld; 8 | typedef vector vi; 9 | #define Fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); 10 | #define endl '\n' 11 | #define sz(s) (int)(s.size()) 12 | #define all(s) s.begin(),s.end() 13 | #define allr(s) s.rbegin(),s.rend() 14 | #define RT(s,v) return cout<> t; 26 | for (int I = 1; I <= t;I++) { 27 | cout << "Case " << I << ": "; 28 | int n; cin >> n; 29 | string s; cin >> s; 30 | int cnt = 0; 31 | for (int i = 0; i < n; i++) { 32 | if (s[i] == '.')cnt++, i += 2; 33 | } 34 | cout << cnt << endl; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /UVa/003/374.cpp: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | #include 4 | using namespace std; 5 | typedef long long ll; 6 | typedef unsigned long long ull; 7 | typedef long double ld; 8 | typedef vector vi; 9 | #define Fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); 10 | #define endl '\n' 11 | #define sz(s) (int)(s.size()) 12 | #define all(s) s.begin(),s.end() 13 | #define allr(s) s.rbegin(),s.rend() 14 | #define RT(s) return cout<> 1, mod) % mod; 25 | return (((r * r) % mod) * power(x, y & 1, mod)) % mod; 26 | } 27 | int main() { 28 | Fast; 29 | file(); 30 | ll x, y, m; 31 | while (cin >> x >> y >> m) { 32 | cout << power(x, y, m) << endl; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /UVa/108/10815.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | #define ll long long 6 | #define endl '\n' 7 | #define RT(v) return cout< st; 28 | while (getline(cin, s)) { 29 | for (char& ch : s) 30 | if (!isalpha(ch))ch = ' '; 31 | else ch = tolower(ch); 32 | stringstream ss(s); 33 | while (ss >> s)st.insert(s); 34 | } 35 | for (auto it : st)cout << it << endl; 36 | } 37 | -------------------------------------------------------------------------------- /UVa/110/11053.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | #define endl "\n" 6 | #define ll long long 7 | #define oo 0x3f3f3f3fLL 8 | #define sz(s) (int)(s.size()) 9 | #define RT(s) return cout<> n, n) { 28 | cin >> a >> b; 29 | map mp; 30 | int cnt = n, x = 0; 31 | while (mp[x] != 3) { 32 | mp[x]++; 33 | cnt -= (mp[x] == 2); 34 | x = ((1LL * a * x) % n * x + b) % n; 35 | } 36 | cout << cnt << endl; 37 | } 38 | } -------------------------------------------------------------------------------- /UVa/104/10469.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | #define endl "\n" 6 | #define ll long long 7 | #define oo 0x3f3f3f3fLL 8 | #define sz(s) (int)(s.size()) 9 | #define RT(s) return cout<> idx) & 1; 26 | } 27 | 28 | int main() { 29 | run(); 30 | ll a, b; 31 | while (cin >> a >> b) { 32 | ll c = 0; 33 | for (int i = 0; i < 32; i++) { 34 | if (getBit(a, i) ^ getBit(b, i)) 35 | c |= (1LL << i); 36 | } 37 | cout << c << endl; 38 | } 39 | } -------------------------------------------------------------------------------- /UVa/002/264.cpp: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | #include 4 | using namespace std; 5 | typedef long long ll; 6 | typedef unsigned long long ull; 7 | typedef long double ld; 8 | typedef vector vi; 9 | #define Fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); 10 | #define endl '\n' 11 | #define sz(s) (int)(s.size()) 12 | #define all(s) s.begin(),s.end() 13 | #define allr(s) s.rbegin(),s.rend() 14 | #define RT(s,v) return cout<> x) { 26 | int n = 0, r; 27 | while ((n * (n + 1) / 2) < x) 28 | n++; 29 | n--; 30 | r = x - n * (n + 1) / 2; 31 | cout << "TERM " << x << " IS "; 32 | if (n & 1) 33 | cout << r << '/' << n - r + 2; 34 | else cout << n - r + 2 << '/' << r; 35 | cout << endl; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /UVa/104/10491.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | #define endl "\n" 6 | #define ll long long 7 | #define oo 0x3f3f3f3fLL 8 | #define sz(s) (int)(s.size()) 9 | #define RT(s) return cout<> cows >> cars >> nshow) { 27 | double p = (cows / (cows + cars)) * (cars / (cows - nshow + cars - 1)); 28 | double p2 = (cars / (cows + cars)) * ((cars - 1) / (cows - nshow + cars - 1)); 29 | cout << fixed << setprecision(5) << p + p2 << endl; 30 | } 31 | } -------------------------------------------------------------------------------- /leetCode/palindrome-partitioning-ii.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string s; 4 | vector> palindrome; 5 | vector mem; 6 | int isPalindrome(int left, int right) { 7 | if (left > right)return 0; 8 | if (left == right || left + 1 == right) 9 | return s[left] == s[right]; 10 | int& rt = palindrome[left][right]; 11 | if (~rt)return rt; 12 | rt = 0; 13 | if (s[left] == s[right]) 14 | rt = isPalindrome(left + 1, right - 1); 15 | return rt; 16 | } 17 | int palindromePartition(int start) { 18 | if (start == (int)(s.size()))return 0; 19 | int& rt = mem[start]; 20 | if (~rt)return rt; 21 | rt = 0x3f3f3f3f; 22 | for (int end = start; end < (int)(s.size()); end++) 23 | if (isPalindrome(start, end)) 24 | rt = min(rt, 1 + palindromePartition(end + 1)); 25 | return rt; 26 | } 27 | int minCut(string s) { 28 | this->s = s; 29 | mem = vector(s.size(), -1); 30 | palindrome = vector>(s.size(), vector(s.size(), -1)); 31 | return palindromePartition(0) - 1; 32 | } 33 | }; 34 | -------------------------------------------------------------------------------- /UVa/120/12095.cpp: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | #include 4 | using namespace std; 5 | typedef long long ll; 6 | typedef unsigned long long ull; 7 | typedef long double ld; 8 | typedef vector vi; 9 | #define Fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); 10 | #define endl '\n' 11 | #define sz(s) (int)(s.size()) 12 | #define all(s) s.begin(),s.end() 13 | #define allr(s) s.rbegin(),s.rend() 14 | #define exit(s) return cout<> n >> m; 27 | map mp; 28 | string s; 29 | while (n--) { cin >> s; cin >> mp[s]; } 30 | while (m--) { 31 | ll cnt = 0; 32 | map b; 33 | while (cin >> s) { 34 | if (s.find('.') != string::npos)break; 35 | cnt += mp[s]; 36 | } 37 | cout << cnt << endl; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /UVa/119/11900.cpp: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | #include 4 | using namespace std; 5 | typedef long long ll; 6 | typedef unsigned long long ull; 7 | typedef long double ld; 8 | typedef vector vi; 9 | #define Fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); 10 | #define endl '\n' 11 | #define sz(s) (int)(s.size()) 12 | #define all(s) s.begin(),s.end() 13 | #define allr(s) s.rbegin(),s.rend() 14 | #define RT(s,v) return cout<> t; 25 | int I = t; 26 | while (t--) { 27 | cout << "Case " << I - t << ": "; 28 | int n, p, q, sum = 0; 29 | cin >> n >> p >> q; 30 | vector v(n); 31 | int i = 0; 32 | while(i < n)cin >> v[i++]; 33 | i = 0; 34 | while (i < n && i < p && sum + v[i] <= q) 35 | sum += v[i++]; 36 | cout << i << endl; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /UVa/004/408.cpp: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | #include 4 | using namespace std; 5 | typedef long long ll; 6 | typedef unsigned long long ull; 7 | typedef long double ld; 8 | typedef vector vi; 9 | #define Fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); 10 | #define endl '\n' 11 | #define sz(s) (int)(s.size()) 12 | #define all(s) s.begin(),s.end() 13 | #define allr(s) s.rbegin(),s.rend() 14 | #define RT(s) return cout<> x >> mod) { 27 | map v; 28 | int s = 0; 29 | do { 30 | v[s] = true; 31 | s = (s + x) % mod; 32 | } while (!v[s]); 33 | cout << setw(10) << x; 34 | cout << setw(10) << mod; 35 | cout << " "; 36 | cout << (sz(v) == mod ? "Good Choice" : "Bad Choice") 37 | << endl << endl; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /UVa/004/494.cpp: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | #include 4 | using namespace std; 5 | typedef long long ll; 6 | typedef unsigned long long ull; 7 | typedef long double ld; 8 | typedef vector vi; 9 | #define Fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); 10 | #define endl '\n' 11 | #define sz(s) (int)(s.size()) 12 | #define all(s) s.begin(),s.end() 13 | #define allr(s) s.rbegin(), s.rend() 14 | #define exit(s) return cout< q; 27 | for (char ch : s) { 28 | ch = tolower(ch); 29 | if ('a' <= ch && ch <= 'z') q.push_back(ch); 30 | else if (!q.empty() && q.back() != ' ') 31 | q.push_back(' '); 32 | } 33 | if (!q.empty() && q.back() == ' ')q.pop_back(); 34 | cout << 1 + count(all(q), ' ') << endl; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /leetCode/scramble-string.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string s1, s2; 4 | vector>> mem; 5 | bool isScramble(int index1, int index2, int length) { 6 | int& rt = mem[index1][index2][length]; 7 | if (~rt)return rt; 8 | rt = 1; 9 | for (int i = 0; i < length; i++) 10 | if (s1[index1 + i] != s2[index2 + i]) { 11 | rt = 0; break; 12 | } 13 | for (int mid = 1; mid < length; mid++) { 14 | rt = rt || (isScramble(index1, index2, mid) 15 | && isScramble(index1 + mid, index2 + mid, length - mid)); 16 | rt = rt || (isScramble(index1 + mid, index2, length - mid) && 17 | isScramble(index1, index2 + length - mid, mid)); 18 | } 19 | return rt; 20 | } 21 | 22 | bool isScramble(string s1, string s2) { 23 | this->s1 = s1; 24 | this->s2 = s2; 25 | int sz = s1.size(); 26 | mem = vector>>(sz, vector>(sz, vector(sz + 1, -1))); 27 | return isScramble(0, 0, sz); 28 | } 29 | }; 30 | -------------------------------------------------------------------------------- /UVa/012/1203.cpp: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | #include 4 | using namespace std; 5 | typedef long long ll; 6 | typedef unsigned long long ull; 7 | typedef long double ld; 8 | typedef vector vi; 9 | #define Fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); 10 | #define endl '\n' 11 | #define sz(s) (int)(s.size()) 12 | #define all(s) s.begin(),s.end() 13 | #define allr(s) s.rbegin(),s.rend() 14 | #define exit(s) return cout< pil; 22 | int main() { 23 | Fast; 24 | file(); 25 | string s; 26 | priority_queue, greater>pq; 27 | while (cin >> s) { 28 | if (s == "#")break; 29 | ll a, x; cin >> a >> x; 30 | for (int i = 1; i <= 10000; i++) 31 | pq.push({ i * x ,a }); 32 | } 33 | int n; cin >> n; 34 | while (n--) { 35 | cout << pq.top().second << endl; 36 | pq.pop(); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /UVa/012/1230.cpp: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | #include 4 | using namespace std; 5 | typedef long long ll; 6 | typedef unsigned long long ull; 7 | typedef long double ld; 8 | typedef vector vi; 9 | #define Fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); 10 | #define endl '\n' 11 | #define sz(s) (int)(s.size()) 12 | #define all(s) s.begin(),s.end() 13 | #define allr(s) s.rbegin(),s.rend() 14 | #define RT(s) return cout<> 1, mod) % mod; 25 | return (((r * r) % mod) * power(x, y & 1, mod)) % mod; 26 | } 27 | int main() { 28 | Fast; 29 | file(); 30 | ll n; 31 | while (cin >> n,n) { 32 | while (n--) { 33 | ll x, y, m; 34 | cin >> x >> y >> m; 35 | cout << power(x, y, m) << endl; 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /UVa/004/483.cpp: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | #include 4 | using namespace std; 5 | typedef long long ll; 6 | typedef unsigned long long ull; 7 | typedef long double ld; 8 | typedef vector vi; 9 | #define Fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); 10 | #define endl '\n' 11 | #define sz(s) (int)(s.size()) 12 | #define all(s) s.begin(),s.end() 13 | #define allr(s) s.rbegin(), s.rend() 14 | #define exit(s) return cout< st; 22 | void outSt() { 23 | while (!st.empty()) { 24 | cout << st.top(); 25 | st.pop(); 26 | } 27 | } 28 | int main() { 29 | Fast; 30 | file(); 31 | string s; 32 | while (getline(cin, s)) { 33 | for (int i = 0; i < sz(s); i++) { 34 | if (s[i] == ' ') { 35 | outSt(); 36 | cout << ' '; 37 | } 38 | else st.push(s[i]); 39 | } 40 | outSt(); 41 | cout << endl; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /UVa/109/10976.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | #define endl "\n" 6 | #define ll long long 7 | #define oo 0x3f3f3f3fLL 8 | #define sz(s) (int)(s.size()) 9 | #define RT(s) return cout<> n) { 31 | vector> v; 32 | for (ll i = 1; i * i <= n * n; i++) if ((n * n) % i == 0) { 33 | v.push_back({ (n * n) / i,i }); 34 | } 35 | cout << sz(v) << endl; 36 | for (auto it : v) 37 | cout << "1/" << n << " = 1/" << it.first + n << " + 1/" << it.second + n << endl; 38 | } 39 | } -------------------------------------------------------------------------------- /UVa/112/11207.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | #define ll long long 5 | #define endl '\n' 6 | #define RT(v) return cout<> n, n) { 28 | int ans = -1;double mx = -oo; 29 | for (int i = 0; i < n; i++) { 30 | double x, y; cin >> x >> y; 31 | if (x < y)swap(x, y); 32 | double size = max(min(x / 4, y), y / 2); 33 | if (mx < size) 34 | mx = size, ans = i + 1; 35 | } 36 | cout << ans << endl; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /UVa/004/414.cpp: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | #include 4 | using namespace std; 5 | typedef long long ll; 6 | typedef unsigned long long ull; 7 | typedef long double ld; 8 | typedef vector vi; 9 | #define Fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); 10 | #define endl '\n' 11 | #define sz(s) (int)(s.size()) 12 | #define all(s) s.begin(),s.end() 13 | #define allr(s) s.rbegin(), s.rend() 14 | #define exit(s) return cout<> n) { 27 | if (n == 0) return 0; 28 | cin.ignore(); 29 | vector v(n); 30 | for (int i = 0; i < n; i++) { 31 | string s; getline(cin, s); 32 | for (char ch : s) 33 | if (ch == 'X') v[i]++; 34 | } 35 | sort(allr(v)); 36 | int mx = 0; 37 | for (int i = 1; i < n; i++) 38 | mx += (v[0] - v[i]); 39 | cout << mx << endl; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /UVa/106/10699.cpp: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | #include 4 | using namespace std; 5 | typedef long long ll; 6 | typedef unsigned long long ull; 7 | typedef long double ld; 8 | typedef vector vi; 9 | #define Fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); 10 | #define endl '\n' 11 | #define sz(s) (int)(s.size()) 12 | #define all(s) s.begin(),s.end() 13 | #define allr(s) s.rbegin(),s.rend() 14 | #define RT(s) return cout< primeFactor(int n) { 23 | map prime; 24 | while (!(n & 1) && n > 0) prime[2]++, n /= 2; 25 | for (int i = 3; i * i <= n; i += 2) 26 | while (!(n % i)) prime[i]++, n /= i; 27 | if (n > 1)prime[n]++; 28 | return prime; 29 | } 30 | int main() { 31 | Fast; 32 | file(); 33 | int n; 34 | while (cin >> n, n) { 35 | cout << n << " : " << sz(primeFactor(n)) << endl; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /UVa/122/12289.cpp: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | #include 4 | using namespace std; 5 | typedef long long ll; 6 | typedef unsigned long long ull; 7 | typedef long double ld; 8 | typedef vector vi; 9 | #define Fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); 10 | #define endl '\n' 11 | #define sz(s) (int)(s.size()) 12 | #define all(s) s.begin(),s.end() 13 | #define allr(s) s.rbegin(), s.rend() 14 | #define exit(s) return cout<> n; 26 | string a[]{ "one","two" }; 27 | while (n--) { 28 | string s; cin >> s; 29 | if (sz(s) == 5)cout << 3 << endl; 30 | else { 31 | int cnt[2] = { 0,0 }; 32 | for (int I = 0; I < 2; I++) 33 | for (int i = 0; i < sz(s); i++) 34 | cnt[I] += (s[i] == a[I][i]); 35 | if (cnt[0] > cnt[1])cout << 1 << endl; 36 | else cout << 2 << endl; 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /UVa/112/11286.cpp: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | #include 4 | using namespace std; 5 | typedef long long ll; 6 | typedef unsigned long long ull; 7 | typedef long double ld; 8 | typedef vector vi; 9 | #define Fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); 10 | #define endl '\n' 11 | #define sz(s) (int)(s.size()) 12 | #define all(s) s.begin(),s.end() 13 | #define allr(s) s.rbegin(),s.rend() 14 | #define exit(s) return cout<> n) { 26 | if (!n)return 0; 27 | map, int> mp; 28 | int mx = 0; 29 | while (n--) { 30 | vector v(5); 31 | int i = 5; 32 | while (i--) cin >> v[i]; 33 | sort(all(v)); 34 | mp[v]++; 35 | mx = max(mx, mp[v]); 36 | } 37 | int cnt = 0; 38 | for (auto it : mp) 39 | if (it.second == mx)cnt += mx; 40 | cout << cnt << endl; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /UVa/012/1260.cpp: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | #include 4 | using namespace std; 5 | typedef long long ll; 6 | typedef unsigned long long ull; 7 | #define endl '\n' 8 | #define clr(v,val) memset(v,val,sizeof(v)) 9 | #define sz(v) (int)(v.size()) 10 | #define all(v) v.begin(),v.end() 11 | #define allr(v) v.rbegin(),v.rend() 12 | #define RT(v,val) return cout<> t; 27 | while (t--) { 28 | int n; cin >> n; 29 | vector v(n); 30 | for (int i = 0; i < n; i++) 31 | cin >> v[i]; 32 | int cnt = 0; 33 | for (int i = 1; i < n; i++) { 34 | for (int j = 0; j < i; j++) { 35 | cnt += (v[i] >= v[j]); 36 | } 37 | } 38 | cout << cnt << endl; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /UVa/116/11679.cpp: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | #include 4 | using namespace std; 5 | typedef long long ll; 6 | typedef unsigned long long ull; 7 | typedef long double ld; 8 | #define Fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); 9 | #define endl '\n' 10 | #define sz(s) (int)(s.size()) 11 | #define all(s) s.begin(),s.end() 12 | #define allr(s) s.rbegin(), s.rend() 13 | #define exit(s) return cout<> b >> n) { 25 | if (!n && !b) return 0; 26 | vector money(b); 27 | for (int i = 0; i < b; i++) 28 | cin >> money[i]; 29 | for (int i = 0; i < n; i++) { 30 | int d, c, v; 31 | cin >> d >> c >> v; 32 | money[d - 1] -= v; 33 | money[c - 1] += v; 34 | } 35 | bool s = 1; 36 | for (int i = 0; s && i < b; i++) 37 | if (money[i] < 0)s = 0; 38 | cout << (s ? 'S' : 'N') << endl; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /UVa/126/12626.cpp: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | #include 4 | using namespace std; 5 | typedef long long ll; 6 | typedef unsigned long long ull; 7 | typedef long double ld; 8 | typedef vector vi; 9 | #define Fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); 10 | #define endl '\n' 11 | #define sz(s) (int)(s.size()) 12 | #define all(s) s.begin(),s.end() 13 | #define allr(s) s.rbegin(), s.rend() 14 | #define exit(s) return cout<> n; 26 | while (n--) { 27 | string s; cin >> s; 28 | string a = "MARGIT"; 29 | map mp; 30 | for (char ch : a) mp[ch] = 0; 31 | for (char ch : s) mp[ch]++; 32 | mp['A'] /= 3, mp['R'] /= 2; 33 | int mn = INT_MAX; 34 | for (auto it : mp) { 35 | if (a.find(it.first) != string::npos) 36 | mn = min(mn, it.second); 37 | } 38 | cout << mn << endl; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /UVa/011/1121.cpp: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | #include 4 | using namespace std; 5 | typedef long long ll; 6 | typedef unsigned long long ull; 7 | typedef long double ld; 8 | typedef vector vi; 9 | #define Fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); 10 | #define endl '\n' 11 | #define sz(s) (int)(s.size()) 12 | #define all(s) s.begin(),s.end() 13 | #define allr(s) s.rbegin(),s.rend() 14 | #define RT(s,v) return cout<> n >> s) { 26 | vector v(n); 27 | for (int i = 0; i < n; i++)cin >> v[i]; 28 | int l = 0, r = 0, mn = INT_MAX; ll sum = 0; 29 | while (l < n) { 30 | while (r < n) { 31 | if (sum >= s) break; 32 | sum += v[r++]; 33 | } 34 | if (sum < s) break; 35 | mn = min(mn, r - l); 36 | sum -= v[l++]; 37 | } 38 | cout << (mn < INT_MAX ? mn : 0) << endl; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /UVa/118/11849.cpp: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | #include 4 | using namespace std; 5 | typedef long long ll; 6 | typedef unsigned long long ull; 7 | typedef long double ld; 8 | typedef vector vi; 9 | #define Fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); 10 | #define endl '\n' 11 | #define sz(s) (int)(s.size()) 12 | #define all(s) s.begin(),s.end() 13 | #define allr(s) s.rbegin(),s.rend() 14 | #define exit(s) return cout<> n >> m) { 27 | if (!n && !m)return 0; 28 | map> mp; 29 | while (n--) { 30 | int a; cin >> a; 31 | mp[a].first = 1; 32 | } 33 | while (m--) { 34 | int a; cin >> a; 35 | mp[a].second = 1; 36 | } 37 | int cnt = 0; 38 | for (auto it : mp) 39 | if (it.second.first&& it.second.second) 40 | cnt++; 41 | cout << cnt << endl; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /UVa/114/11420.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | #define ll long long 5 | #define endl '\n' 6 | #define RT(v) return cout<> n >> s, n >= 0 && s >= 0) { 40 | cout << solve(n, s, false) << endl; 41 | } 42 | } -------------------------------------------------------------------------------- /UVa/128/12869.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | #define endl "\n" 6 | #define ll long long 7 | #define sz(s) (int)(s.size()) 8 | #define INF 0x3f3f3f3f3f3f3f3fLL 9 | #define all(v) v.begin(),v.end() 10 | #define watch(x) cout<<(#x)<<" = "<= 201402L 14 | template 15 | vector create(size_t n) { 16 | return vector(n); 17 | } 18 | template 19 | auto create(size_t n, Args ... args) { 20 | return vector(args...))>(n, create(args...)); 21 | } 22 | #endif 23 | void run() { 24 | ios::sync_with_stdio(false); 25 | cin.tie(NULL); 26 | cout.tie(NULL); 27 | #ifdef EZZAT 28 | freopen("input.in", "r", stdin); 29 | //freopen("output.out", "w", stdout); 30 | #else 31 | #endif 32 | } 33 | 34 | ll cnt(ll n) { 35 | return n / 5; 36 | } 37 | 38 | int main() { 39 | run(); 40 | ll l, r; 41 | while (cin >> l >> r, l && r) { 42 | cout << cnt(r) - cnt(l) + 1 << endl; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /spoj/ELIS.cpp: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | #include 4 | using namespace std; 5 | typedef long long ll; 6 | typedef unsigned long long ull; 7 | #define endl '\n' 8 | #define clr(v,val) memset(v,val,sizeof(v)) 9 | #define sz(v) (int)(v.size()) 10 | #define all(v) v.begin(),v.end() 11 | #define allr(v) v.rbegin(),v.rend() 12 | #define RT(v,val) return cout< v; 25 | int mxSub(int ind, int last) { 26 | if (ind >= n)return 0; 27 | int c = mxSub(ind + 1, last); 28 | if (v[ind] > last) { 29 | c = max(c, 1 + mxSub(ind + 1, v[ind])); 30 | } 31 | return c; 32 | } 33 | 34 | int main() { 35 | run(); 36 | cin >> n; 37 | v.resize(n); 38 | for (int i = 0; i < n; i++) 39 | cin >> v[i]; 40 | cout << mxSub(0, 0); 41 | } 42 | -------------------------------------------------------------------------------- /UVa/004/460.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | #define ll long long 5 | #define endl '\n' 6 | #define RT(v) return cout<> t; 26 | while (t--) { 27 | int xl, yd, xr, yu, x1, y1, x2, y2; 28 | cin >> xl >> yd >> xr >> yu >> x1 >> y1 >> x2 >> y2; 29 | xl = max(xl, x1); yd = max(yd, y1); 30 | xr = min(xr, x2); yu = min(yu, y2); 31 | if (yu <= yd || xr <= xl)cout << "No Overlap"; 32 | else cout << xl << ' ' << yd << ' ' << xr << ' ' << yu; 33 | cout << endl; 34 | if (t) cout << endl; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /spoj/TRT.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | #define ll long long 6 | #define endl '\n' 7 | #define RT(v) return cout< v; 25 | int mem[2009][2009]; 26 | int solve(int i, int j) { 27 | if (j < i)return 0; 28 | int& rt = mem[i][j]; 29 | if (~rt)return rt; 30 | int a = i + sz(v) - j; 31 | return rt = max(a * v[i] + solve(i + 1, j), 32 | a * v[j] + solve(i, j - 1)); 33 | } 34 | 35 | int main() { 36 | run(); 37 | clr(mem, -1); 38 | int n; cin >> n; 39 | v.resize(n); 40 | for (int i = 0; i < n; i++)cin >> v[i]; 41 | cout << solve(0, sz(v) - 1) << endl; 42 | } 43 | -------------------------------------------------------------------------------- /UVa/108/10843.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | #define ll long long 5 | #define endl '\n' 6 | #define RT(v) return cout<> 1, mod); 28 | return (((r * r) % mod) * power(x, y & 1, mod)) % mod; 29 | } 30 | 31 | int main() { 32 | run(); 33 | int t; 34 | while (cin >> t) { 35 | for (int I = 1; I <= t; I++) { 36 | int n; cin >> n; 37 | cout << "Case #" << I << ": " << (n < 3 ? 1 : power(n, n - 2, 2000000011)) << endl; 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /UVa/109/10945.cpp: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | #include 4 | using namespace std; 5 | typedef long long ll; 6 | typedef unsigned long long ull; 7 | typedef long double ld; 8 | typedef vector vi; 9 | #define Fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); 10 | #define endl '\n' 11 | #define sz(s) (int)(s.size()) 12 | #define all(s) s.begin(),s.end() 13 | #define allr(s) s.rbegin(), s.rend() 14 | #define exit(s) return cout< 3 | #include 4 | using namespace std; 5 | typedef long long ll; 6 | typedef unsigned long long ull; 7 | typedef long double ld; 8 | typedef vector vi; 9 | #define Fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); 10 | #define endl '\n' 11 | #define sz(s) (int)(s.size()) 12 | #define all(s) s.begin(),s.end() 13 | #define allr(s) s.rbegin(),s.rend() 14 | #define RT(s,v) return cout<> n, n) { 26 | vector v(n); 27 | for (int i = 0; i < n; i++)cin >> v[i]; 28 | sort(all(v)); 29 | priority_queue,greater> q; 30 | int cnt = 0; 31 | for (int i = 0; i < n; i++) q.push(v[i]); 32 | while (!q.empty()) { 33 | ll x = q.top(); 34 | q.pop(); 35 | if (q.empty()) break; 36 | ll y = q.top(); 37 | q.pop(); 38 | cnt += x + y; 39 | q.push(x + y); 40 | } 41 | cout << cnt << endl; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /UVa/005/541.cpp: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | #include 4 | using namespace std; 5 | typedef long long ll; 6 | typedef unsigned long long ull; 7 | typedef long double ld; 8 | typedef vector vi; 9 | #define Fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); 10 | #define endl '\n' 11 | #define sz(s) (int)(s.size()) 12 | #define all(s) s.begin(),s.end() 13 | #define allr(s) s.rbegin(),s.rend() 14 | #define RT(s,v) return cout<> t; 26 | while (t--) { 27 | int n, m; 28 | cin >> n >> m; 29 | vector v(n); 30 | while (n--)cin >> v[n]; 31 | bool b = 0; 32 | n = sz(v); 33 | for (int i = 1; i < (1 << n); i++) { 34 | bitset<30>bit(i); 35 | int sum = 0; 36 | for (int j = 0; j < n; j++) { 37 | if (bit[j]) sum += v[j]; 38 | if (sum >= m) break; 39 | } 40 | if (sum == m) { 41 | b = 1; break; 42 | } 43 | } 44 | cout << (b ? "Yes" : "No") << endl; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /UVa/119/11933.cpp: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | #include 4 | using namespace std; 5 | typedef long long ll; 6 | typedef unsigned long long ull; 7 | typedef long double ld; 8 | typedef vector vi; 9 | #define Fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); 10 | #define endl '\n' 11 | #define sz(s) (int)(s.size()) 12 | #define all(s) s.begin(),s.end() 13 | #define allr(s) s.rbegin(),s.rend() 14 | #define RT(s,v) return cout<> t; 26 | while (t--) { 27 | int n, m; 28 | cin >> n >> m; 29 | vector v(n); 30 | while (n--)cin >> v[n]; 31 | bool b = 0; 32 | n = sz(v); 33 | for (int i = 1; i < (1 << n); i++) { 34 | bitset<30>bit(i); 35 | int sum = 0; 36 | for (int j = 0; j < n; j++) { 37 | if (bit[j]) sum += v[j]; 38 | if (sum >= m) break; 39 | } 40 | if (sum == m) { 41 | b = 1; break; 42 | } 43 | } 44 | cout << (b ? "Yes" : "No") << endl; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /UVa/106/10617.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | #define ll long long 6 | #define endl '\n' 7 | #define RT(v) return cout<> t; 40 | while (t--) { 41 | clr(mem, -1); 42 | cin >> s; 43 | cout << count(0, sz(s) - 1) << endl; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /UVa/109/10935.cpp: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | #include 4 | using namespace std; 5 | typedef long long ll; 6 | typedef unsigned long long ull; 7 | typedef long double ld; 8 | typedef vector vi; 9 | #define Fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); 10 | #define endl '\n' 11 | #define sz(s) (int)(s.size()) 12 | #define all(s) s.begin(),s.end() 13 | #define allr(s) s.rbegin(),s.rend() 14 | #define exit(s) return cout<> n) { 27 | if (n == 0) return 0; 28 | deque q(n); 29 | for (int i = 0; i < n; i++) q[i] = i + 1; 30 | cout << "Discarded cards:"; 31 | while (sz(q) > 2) { 32 | cout << " " << q.front() << ","; 33 | q.pop_front(); 34 | q.push_back(q.front()); 35 | q.pop_front(); 36 | } 37 | if (sz(q) == 2) { cout << " " << q.front(); q.pop_front(); } 38 | cout << endl << "Remaining card: "; 39 | if (sz(q) == 1) cout << q.front(); 40 | cout << endl; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /UVa/002/294.cpp: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | #include 4 | using namespace std; 5 | typedef long long ll; 6 | typedef unsigned long long ull; 7 | typedef long double ld; 8 | typedef vector vi; 9 | #define Fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); 10 | #define endl '\n' 11 | #define sz(s) (int)(s.size()) 12 | #define all(s) s.begin(),s.end() 13 | #define allr(s) s.rbegin(),s.rend() 14 | #define RT(s) return cout<> t) { 33 | while (t--) { 34 | int l, u, x, mx = 0; 35 | cin >> l >> u; 36 | for (int i = l; i <= u; i++) { 37 | int cnt = divisors(i); 38 | if (cnt > mx) mx = cnt, x = i; 39 | } 40 | printf("Between %d and %d, %d has a maximum of %d divisors.\n", l, u, x, mx); 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /UVa/005/514.cpp: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | #include 4 | using namespace std; 5 | typedef long long ll; 6 | typedef unsigned long long ull; 7 | typedef long double ld; 8 | typedef vector vi; 9 | #define Fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); 10 | #define endl '\n' 11 | #define sz(s) (int)(s.size()) 12 | #define all(s) s.begin(),s.end() 13 | #define allr(s) s.rbegin(),s.rend() 14 | #define exit(s) return cout<> n, n) { 26 | int a; 27 | while (cin >> a) { 28 | if (!a) { cout << endl; break; } 29 | vector v(n); 30 | v[0] = a; 31 | for (int i = 1; i < n; i++)cin >> v[i]; 32 | stack st; 33 | int c = 1, i = 0; 34 | while (c <= n) { 35 | st.push(c); 36 | c++; 37 | while (!st.empty() && st.top() == v[i]) { 38 | st.pop(); 39 | i++; 40 | if (i >= n) break; 41 | } 42 | } 43 | cout << (st.empty() ? "Yes" : "No") << endl; 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /UVa/004/417.cpp: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | #include 4 | using namespace std; 5 | typedef long long ll; 6 | typedef unsigned long long ull; 7 | typedef long double ld; 8 | typedef vector vi; 9 | #define Fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); 10 | #define endl '\n' 11 | #define sz(s) (int)(s.size()) 12 | #define all(s) s.begin(),s.end() 13 | #define allr(s) s.rbegin(),s.rend() 14 | #define exit(s) return cout< mp; 40 | for (int i = 1; i <= 83681; i++) { 41 | mp[s] = i; 42 | pls(s, sz(s) - 1, 'z'); 43 | } 44 | s.clear(); 45 | while (cin >> s) { cout << mp[s] << endl; } 46 | } 47 | -------------------------------------------------------------------------------- /UVa/102/10264.cpp: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | #include 4 | using namespace std; 5 | typedef long long ll; 6 | typedef unsigned long long ull; 7 | typedef long double ld; 8 | typedef vector vi; 9 | #define Fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); 10 | #define endl '\n' 11 | #define sz(s) (int)(s.size()) 12 | #define all(s) s.begin(),s.end() 13 | #define allr(s) s.rbegin(),s.rend() 14 | #define RT(s,v) return cout<> n) { 27 | vector v(1 << n); 28 | vector corner(1 << n); 29 | for (int i = 0; i < (1 << n); i++) 30 | cin >> v[i]; 31 | for (int i = 0; i < (1 << n); i++) 32 | for (int j = 0; j < n; j++) 33 | corner[i] += v[flipBit(i, j)]; 34 | int mx = 0; 35 | for (int i = 0; i < (1 << n); i++) 36 | for (int j = 0; j < n; j++) 37 | mx = max(mx, corner[i] + corner[flipBit(i, j)]); 38 | cout << mx << endl; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /UVa/113/11345.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | #define ll long long 5 | #define endl '\n' 6 | #define RT(v) return cout<> t; 27 | for (int I = 1; I <= t; I++) { 28 | int n; cin >> n; 29 | int x, xx, y, yy; 30 | cin >> x >> y >> xx >> yy; n--; 31 | while (n--) { 32 | int x1, x2, y1, y2; 33 | cin >> x1 >> y1 >> x2 >> y2; 34 | x = max(x, x1); 35 | y = max(y, y1); 36 | xx = min(xx, x2); 37 | yy = min(yy, y2); 38 | } 39 | cout << "Case " << I << ": "; 40 | if (yy < y || xx < x)cout << 0; 41 | else cout << (xx - x) * (yy - y); 42 | cout << endl; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /UVa/005/562.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | #define ll long long 5 | #define endl '\n' 6 | #define RT(v) return cout<> t; 26 | while (t--) { 27 | int n; 28 | cin >> n; 29 | vector v(n); 30 | int mx = 0; 31 | for (int i = 0; i < n; i++) { cin >> v[i]; mx += v[i]; } 32 | vector> dp(n + 1, vector(mx + 1)); 33 | dp[0][0] = 1; 34 | for (int i = 0; i < n; i++) for (int d = 0; d <= mx; d++) 35 | if (dp[i][d]) { 36 | dp[i + 1][d + v[i]] = 1; 37 | dp[i + 1][abs(d - v[i])] = 1; 38 | } 39 | for (int i = 0; i <= mx; i++) if (dp[n][i]) { 40 | cout << i << endl; 41 | break; 42 | } 43 | } 44 | } -------------------------------------------------------------------------------- /UVa/001/128.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | #define endl "\n" 6 | #define ll long long 7 | #define oo 0x3f3f3f3fLL 8 | #define sz(s) (int)(s.size()) 9 | #define RT(s) return cout<= 0; i--) { 38 | if (i == 2)continue; 39 | int r = sum % 16; 40 | if (0 <= r && r < 10) 41 | hex[i] = r + '0'; 42 | else hex[i] = r - 10 + 'A'; 43 | sum /= 16; 44 | } 45 | cout << hex << endl; 46 | } 47 | 48 | } -------------------------------------------------------------------------------- /spoj/PT07Y.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | #define ll long long 5 | #define endl '\n' 6 | #define RT(v) return cout<> adj; 24 | bitset<10001> vis; 25 | void dfs(int node) { 26 | vis[node] = 1; 27 | for (int child : adj[node]) { 28 | if (!vis[child])dfs(child); 29 | } 30 | } 31 | int main() { 32 | run(); 33 | int n, e; 34 | cin >> n >> e; 35 | if (e != n - 1)RT("NO"); 36 | adj = vector>(n + 1); 37 | while (e--) { 38 | int u, v; 39 | cin >> u >> v; 40 | adj[u].push_back(v); 41 | adj[v].push_back(u); 42 | } 43 | dfs(1); 44 | for (int i = 2; i <= n; i++) 45 | if (!vis[i])RT("NO"); 46 | RT("YES"); 47 | } 48 | -------------------------------------------------------------------------------- /UVa/105/10509.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | #define endl "\n" 6 | #define ll long long 7 | #define oo 0x3f3f3f3fLL 8 | #define sz(s) (int)(s.size()) 9 | #define RT(s) return cout<> n,n > 1e-9){ 40 | int i = pow(n + 1e-6,1.0/3); 41 | double dx = (n - i*i*i)/(3*i*i); 42 | cout << fixed << setprecision(4) << i+dx << endl; 43 | } 44 | } -------------------------------------------------------------------------------- /UVa/110/11026.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | #define ll long long 5 | #define endl '\n' 6 | #define RT(v) return cout< v; 25 | ll mem[1001][1001]; 26 | ll solve(int idx, int k) { 27 | if (k == 0)return 1; 28 | if (idx >= n - (k - 1))return 0; 29 | ll& rt = mem[idx][k]; 30 | if (~rt)return rt; 31 | return rt = (((v[idx] * solve(idx + 1, k - 1)) % m) + solve(idx + 1, k)) % m; 32 | } 33 | 34 | int main() { 35 | run(); 36 | while (cin >> n >> m, n || m) { 37 | clr(mem, -1); 38 | v = vector(n); 39 | for (int i = 0; i < n; i++) 40 | cin >> v[i]; 41 | ll mx = 0; 42 | for (int k = n; k > 0; k--) 43 | mx = max(mx, solve(0, k)); 44 | cout << mx << endl; 45 | } 46 | } -------------------------------------------------------------------------------- /spoj/ONP.cpp: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | #include 4 | using namespace std; 5 | typedef long long ll; 6 | typedef unsigned long long ull; 7 | typedef long double ld; 8 | typedef vector vi; 9 | #define Fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); 10 | #define endl '\n' 11 | #define sz(s) (int)(s.size()) 12 | #define all(s) s.begin(),s.end() 13 | #define allr(s) s.rbegin(),s.rend() 14 | #define exit(s) return cout<& st) { 23 | string s[3]; 24 | int n = 3; 25 | while (n--) { 26 | s[n] = st.top(); 27 | st.pop(); 28 | } 29 | st.pop(); 30 | string y = s[0] + s[2] + s[1]; 31 | return y; 32 | } 33 | int main() { 34 | Fast; 35 | file(); 36 | int n; cin >> n; 37 | while (n--) { 38 | string s; cin >> s; 39 | stack st; 40 | for (int i = 0; i < sz(s); i++) { 41 | string y; 42 | if (s[i] == ')') y = con(st); 43 | else y.push_back(s[i]); 44 | st.push(y); 45 | } 46 | while (!st.empty()) { 47 | cout << st.top() << endl; 48 | st.pop(); 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /UVa/004/498.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | #define endl "\n" 6 | #define ll long long 7 | #define oo 0x3f3f3f3fLL 8 | #define sz(s) (int)(s.size()) 9 | #define RT(s) return cout< input() { 25 | string s; getline(cin, s); 26 | if (s.empty())exit(0); 27 | stringstream ss; ss << s; 28 | vector v; 29 | int a; 30 | while (ss >> a) v.push_back(a); 31 | return v; 32 | } 33 | 34 | int main() { 35 | run(); 36 | while (true) { 37 | vector c = input(), x = input(); 38 | reverse(all(c)); 39 | for (int i = 0; i < x.size(); i++) { 40 | ll res = 0, curX = 1; 41 | for (int j = 0; j < c.size(); j++) { 42 | res += c[j] * curX; 43 | curX *= x[i]; 44 | } 45 | cout << res << " \n"[i + 1 == x.size()]; 46 | } 47 | } 48 | } -------------------------------------------------------------------------------- /UVa/013/1346.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | #define ll long long 6 | #define endl '\n' 7 | #define RT(v) return cout<> n) { 29 | vector>>v(n); 30 | for (auto& a : v)cin >> a.first >> a.second.first >> a.second.second; 31 | sort(all(v), 32 | [](const pair>& a, const pair>& b) { 33 | double costAB = b.second.second * a.second.first, 34 | costBA = a.second.second * b.second.first; 35 | return costAB < costBA; 36 | }); 37 | int k; cin >> k; 38 | cout << v[k - 1].first << endl; 39 | } 40 | } -------------------------------------------------------------------------------- /UVa/116/11661.cpp: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | #include 4 | using namespace std; 5 | typedef long long ll; 6 | typedef unsigned long long ull; 7 | typedef long double ld; 8 | typedef vector vi; 9 | #define Fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); 10 | #define endl '\n' 11 | #define sz(s) (int)(s.size()) 12 | #define all(s) s.begin(),s.end() 13 | #define allr(s) s.rbegin(), s.rend() 14 | #define exit(s) return cout<> n) { 27 | if (!n) return 0; 28 | string s; cin >> s; 29 | if (s.find('Z') != string::npos) 30 | cout << 0 << endl; 31 | else { 32 | int mn = INT_MAX; 33 | stack r, d; 34 | for (int i = 0; i < n; i++) { 35 | if (s[i] == 'R') { 36 | r.push(i); 37 | if (!d.empty()) mn = min(mn, abs(d.top() - r.top())); 38 | } 39 | else if (s[i] == 'D') { 40 | d.push(i); 41 | if (!r.empty()) mn = min(mn, abs(d.top() - r.top())); 42 | } 43 | } 44 | cout << mn << endl; 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /UVa/119/11988.cpp: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | #include 4 | using namespace std; 5 | typedef long long ll; 6 | typedef unsigned long long ull; 7 | typedef long double ld; 8 | typedef vector vi; 9 | #define Fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); 10 | #define endl '\n' 11 | #define sz(s) (int)(s.size()) 12 | #define all(s) s.begin(),s.end() 13 | #define allr(s) s.rbegin(), s.rend() 14 | #define exit(s) return cout<q; 29 | stack st; 30 | for (char ch : s) { 31 | if (ch == ']' || ch == '[') { 32 | h = (ch == '['); 33 | while (!st.empty()) { 34 | q.push_front(st.top()); 35 | st.pop(); 36 | } 37 | } 38 | else { 39 | if (h) st.push(ch); 40 | else q.push_back(ch); 41 | } 42 | } 43 | while (!st.empty()) { 44 | q.push_front(st.top()); 45 | st.pop(); 46 | } 47 | for (int i = 0; i < sz(q); i++) 48 | cout << q[i]; 49 | cout << endl; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /TopCoder/BestView.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | #define ll long long 6 | #define oo 0x3f3f3f3fLL 7 | #define sz(s) (int)(s.size()) 8 | #define RT(s) return cout<> n, n != 0) { 37 | cout << setw(5) << n; 38 | cout << " "; 39 | int sum = sumDiv(n); 40 | if (n == sum)cout << "PERFECT"; 41 | else if (n > sum) cout << "DEFICIENT"; 42 | else cout << "ABUNDANT"; 43 | cout << endl; 44 | } 45 | cout << "END OF OUTPUT" << endl; 46 | } 47 | -------------------------------------------------------------------------------- /UVa/103/10325.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | #define endl "\n" 6 | #define ll long long 7 | #define oo 0x3f3f3f3fLL 8 | #define sz(s) (int)(s.size()) 9 | #define RT(s) return cout< v; 34 | int solve(int idx = 0, int cnt = 0, ll num = 1) { 35 | if (num > n)return 0; 36 | if (idx == m) { 37 | return n / num * ((cnt & 1) ? -1 : 1); 38 | } 39 | return solve(idx + 1, cnt, num) + solve(idx + 1, cnt + 1, lcm(v[idx], num)); 40 | } 41 | 42 | int main() { 43 | run(); 44 | while (cin >> n >> m) { 45 | v = vector(m); 46 | for (int i = 0; i < m; i++) cin >> v[i]; 47 | cout << solve() << endl; 48 | } 49 | } -------------------------------------------------------------------------------- /UVa/120/12032.cpp: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | #include 4 | using namespace std; 5 | typedef long long ll; 6 | typedef unsigned long long ull; 7 | typedef long double ld; 8 | typedef vector vi; 9 | #define Fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); 10 | #define endl '\n' 11 | #define sz(s) (int)(s.size()) 12 | #define all(s) s.begin(),s.end() 13 | #define allr(s) s.rbegin(),s.rend() 14 | #define RT(s,v) return cout< v; 23 | bool ok(ll k) { 24 | for (int i = 1; i <= n; i++) { 25 | if (v[i - 1] + k == v[i])k--; 26 | else if (v[i - 1] + k < v[i]) return 0; 27 | } 28 | return 1; 29 | } 30 | int main() { 31 | Fast; 32 | file(); 33 | int t; cin >> t; 34 | int I = t; 35 | while (t--) { 36 | cout << "Case " << I - t << ": "; 37 | cin >> n; 38 | v.clear(); v.resize(n + 2); 39 | for (int i = 1; i <= n; i++)cin >> v[i]; 40 | ll st = 0, ed = 1e10, mid, k = INT_MAX; 41 | while (st <= ed) { 42 | mid = (ed + st) >> 1; 43 | if (ok(mid)) ed = mid - 1, k = mid; 44 | else st = mid + 1; 45 | } 46 | cout << k << endl; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /UVa/100/10036.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | #define ll long long 5 | #define endl '\n' 6 | #define RT(v) return cout<> t; 30 | while (t--) { 31 | int n, k; 32 | cin >> n >> k; 33 | vector v(n); 34 | for (int i = 0; i < n; i++)cin >> v[i]; 35 | vector> dp(n + 1, vector(k)); 36 | dp[n][0] = 1; 37 | for (int i = n - 1; i >= 0; i--) 38 | for (int rem = 0; rem < k; rem++) { 39 | dp[i][rem] |= dp[i + 1][fixMod(rem + v[i], k)]; 40 | dp[i][rem] |= dp[i + 1][fixMod(rem - v[i], k)]; 41 | } 42 | if (dp[0][0])cout << "Divisible\n"; 43 | else cout << "Not divisible\n"; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /UVa/110/11062.cpp: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | #include 4 | using namespace std; 5 | typedef long long ll; 6 | typedef unsigned long long ull; 7 | typedef long double ld; 8 | typedef vector vi; 9 | #define Fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); 10 | #define endl '\n' 11 | #define sz(s) (int)(s.size()) 12 | #define all(s) s.begin(),s.end() 13 | #define allr(s) s.rbegin(), s.rend() 14 | #define exit(s) return cout< st; 28 | while (cin >> s) { 29 | if (!x.empty()) { s = x + s; x.clear(); } 30 | if (s.back() == '-') { s.pop_back(); x = s; continue; } 31 | string y; 32 | for (char& ch : s) { 33 | ch = tolower(ch); 34 | if (!isChar(ch) && ch != '-') ch = ' '; 35 | } 36 | for (char ch : s) { 37 | if (ch != ' ') y.push_back(ch); 38 | else if (!y.empty()) 39 | st.insert(y), y.clear(); 40 | } 41 | if (!y.empty()) 42 | st.insert(y), y.clear();; 43 | } 44 | for (auto it : st) 45 | cout << it << endl; 46 | } 47 | -------------------------------------------------------------------------------- /TopCoder/UnsealTheSafe.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | #define endl "\n" 6 | #define ll long long 7 | #define oo 0x3f3f3f3fLL 8 | #define sz(s) (int)(s.size()) 9 | #define RT(s) return cout<> t; 26 | while (t--) { 27 | ll g, l; cin >> g >> l; 28 | if (l% g)cout << -1 << endl; 29 | else { 30 | bool b = 0; 31 | ll n = l * g; 32 | pair p = { -1,-1 }; 33 | for (ll i = g; i >= 1; i--) { 34 | if (n % i == 0) { 35 | ll gc = gcd(i, n / i); 36 | ll lc = i / gc * (n / i); 37 | if (gc == g && lc == l) { 38 | p.first = i; 39 | p.second = n / i; 40 | break; 41 | } 42 | } 43 | } 44 | cout << p.first; 45 | if(p.second != -1)cout << ' ' << p.second << endl; 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /UVa/119/11991.cpp: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | #include 4 | using namespace std; 5 | typedef long long ll; 6 | typedef unsigned long long ull; 7 | #define endl '\n' 8 | #define clr(v,val) memset(v,val,sizeof(v)) 9 | #define sz(v) (int)(v.size()) 10 | #define all(v) v.begin(),v.end() 11 | #define allr(v) v.rbegin(),v.rend() 12 | #define RT(v,val) return cout<> n >> m) { 31 | vector v(n + 9); 32 | map> mp; 33 | for (int i = 1; i <= n; i++) 34 | cin >> v[i]; 35 | for (int i = 1; i <= n; i++) { 36 | if (mp[v[i]].empty()) 37 | mp[v[i]].push_back(0); 38 | mp[v[i]].push_back(i); 39 | } 40 | while (m--) { 41 | int k, x; 42 | cin >> k >> x; 43 | if (mp[x].empty() || sz(mp[x]) <= k) 44 | cout << 0 << endl; 45 | else 46 | cout << mp[x][k] << endl; 47 | } 48 | 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /UVa/012/1209.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | #define ll long long 5 | #define endl '\n' 6 | #define RT(v) return cout< dif(b); 32 | return a < b; 33 | } 34 | 35 | int main() { 36 | run(); 37 | string s; 38 | while (cin >> s) { 39 | string ans = s; 40 | string t = s; 41 | int cnt = 10; 42 | while (cnt-- && next_permutation(all(t))) 43 | if (cmp(t, ans))ans = t; 44 | t = s; 45 | cnt = 10; 46 | while (cnt-- && prev_permutation(all(t))) 47 | if (cmp(t, ans))ans = t; 48 | cout << ans << dif(ans) << endl; 49 | } 50 | } -------------------------------------------------------------------------------- /UVa/108/10892.cpp: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | #include 4 | using namespace std; 5 | typedef long long ll; 6 | typedef unsigned long long ull; 7 | typedef long double ld; 8 | typedef vector vi; 9 | #define Fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); 10 | #define endl '\n' 11 | #define sz(s) (int)(s.size()) 12 | #define all(s) s.begin(),s.end() 13 | #define allr(s) s.rbegin(),s.rend() 14 | #define RT(s) return cout< divisors(ll n) { 22 | vector v; ll i; 23 | for (i = 1; i * i < n; i++) 24 | if (!(n% i)) v.push_back(i), v.push_back(n / i); 25 | if (i* i == n) v.push_back(i); 26 | sort(all(v)); 27 | return v; 28 | } 29 | ll gcd(ll a, ll b) { return !b ? abs(a) : gcd(b, a % b); } 30 | 31 | ll lcm(ll a, ll b) { return abs(a / gcd(a, b)) * b; } 32 | 33 | int main() { 34 | Fast; 35 | file(); 36 | ll n; 37 | while (cin >> n, n) { 38 | vector v = divisors(n); 39 | ll cnt = 0; 40 | for (int i = 0; i < sz(v); i++) { 41 | for (int j = i; j < sz(v); j++) { 42 | if (lcm(v[i], v[j]) == n) 43 | cnt++; 44 | } 45 | } 46 | cout << n << ' ' << cnt << endl; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /UVa/103/10344.cpp: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | #include 4 | using namespace std; 5 | typedef long long ll; 6 | typedef unsigned long long ull; 7 | #define endl '\n' 8 | #define clr(v,val) memset(v,val,sizeof(v)) 9 | #define sz(v) (int)(v.size()) 10 | #define all(v) v.begin(),v.end() 11 | #define allr(v) v.rbegin(),v.rend() 12 | #define RT(v,val) return cout< v(5); 24 | bool out23(int ind, int res) { 25 | if (ind > 4) return res == 23; 26 | return (out23(ind + 1, res + v[ind]) || 27 | out23(ind + 1, res - v[ind]) || 28 | out23(ind + 1, res * v[ind])); 29 | } 30 | 31 | int main() { 32 | run(); 33 | while (cin >> v[0], v[0]) { 34 | for (int i = 1; i < 5; i++) 35 | cin >> v[i]; 36 | bool get = 0; 37 | sort(all(v)); 38 | do { 39 | if (out23(1, v[0])) { 40 | get = 1; 41 | break; 42 | } 43 | } while (next_permutation(all(v))); 44 | if (get)cout << "Possible" << endl; 45 | else cout << "Impossible" << endl; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /UVa/006/616.cpp: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | #include 4 | using namespace std; 5 | typedef long long ll; 6 | typedef unsigned long long ull; 7 | typedef long double ld; 8 | typedef vector vi; 9 | #define Fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); 10 | #define endl '\n' 11 | #define sz(s) (int)(s.size()) 12 | #define all(s) s.begin(),s.end() 13 | #define allr(s) s.rbegin(),s.rend() 14 | #define RT(s,v) return cout<> 1); 25 | return r * r * power(x, y & 1); 26 | } 27 | int main() { 28 | Fast; 29 | file(); 30 | ll n; 31 | while (cin >> n, n > 0) { 32 | cout << n << " coconuts, "; 33 | ll p = -1, x; 34 | for (x = 2; floor(x * log(x)) <= log(n); x++) { 35 | ll nn = n, I = x; 36 | bool b = 1; 37 | while (I--) { 38 | if (nn% x == 1) { 39 | nn /= x; 40 | nn *= (x - 1); 41 | } 42 | else { b = 0; break; } 43 | } 44 | if (b&& nn% x == 0) p = x; 45 | } 46 | if (p > 0) 47 | cout << p << " people and 1 monkey" << endl; 48 | else cout << "no solution" << endl; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /UVa/100/10003.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | #define ll long long 5 | #define endl '\n' 6 | #define RT(v) return cout< point; 24 | int mem[59][59]; 25 | int cut(int i, int j) { 26 | if (j < i)return 0; 27 | int s = point[i - 1], e = point[j + 1]; 28 | if (j == i)return e - s; 29 | int& rt = mem[i][j]; 30 | if (~rt)return rt; 31 | rt = oo; 32 | int cost = e - s; 33 | for (int k = i; k <= j; k++) 34 | rt = min(rt, cut(i, k - 1) + cut(k + 1, j)); 35 | return rt += cost; 36 | } 37 | 38 | int main() { 39 | run(); 40 | while (cin >> l, l) { 41 | clr(mem, -1); 42 | cin >> n; 43 | point = vector(n + 1); 44 | for (int i = 1; i <= n; i++) cin >> point[i]; 45 | point.push_back(l); 46 | cout << "The minimum cutting is " << cut(1, n) << "." << endl; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /UVa/103/10368.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | #define endl "\n" 6 | #define ll long long 7 | #define oo 0x3f3f3f3fLL 8 | #define sz(s) (int)(s.size()) 9 | #define RT(s) return cout< 1); 40 | } 41 | 42 | int main() { 43 | run(); 44 | ll a, b; 45 | while (cin >> a >> b, a || b) { 46 | if (a < b) 47 | swap(a, b); 48 | if (solve(a, b)) 49 | cout << "Stan wins\n"; 50 | else 51 | cout << "Ollie wins\n"; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /TIMUS/1350.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | #define ll long long 6 | #define endl '\n' 7 | #define RT(v) return cout<> n; 27 | set st; 28 | while (n--) { 29 | string s; cin >> s; st.insert(s); 30 | } 31 | cin >> n; 32 | vector> v(n + 1); 33 | for (int i = 0; i <= n; i++) { 34 | int m; cin >> m; 35 | while (m--) { 36 | string s; cin >> s; 37 | v[i].push_back(s); 38 | } 39 | } 40 | int m; cin >> m; 41 | for (auto it : v[0])st.erase(it); 42 | for (int i = 1; i <= n; i++) { 43 | int cnt = 0; 44 | for (auto it : v[i]) { 45 | if (st.find(it) != st.end())cnt++; 46 | } 47 | if (!cnt)cout << "YES\n"; 48 | else if (sz(st) - cnt >= m)cout << "MAYBE\n"; 49 | else cout << "NO\n"; 50 | } 51 | } -------------------------------------------------------------------------------- /UVa/115/11518.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | #define ll long long 5 | #define endl '\n' 6 | #define RT(v) return cout< vis; 24 | vector> adj; 25 | int dfs(int node) { 26 | if (vis[node])return 0; 27 | vis[node] = 1; 28 | int c = 1; 29 | for (int child : adj[node]) 30 | c += dfs(child); 31 | return c; 32 | } 33 | int main() { 34 | run(); 35 | int t; cin >> t; 36 | while (t--) { 37 | int n, m, l; 38 | cin >> n >> m >> l; 39 | adj = vector>(n + 1); 40 | vis = vector(n + 1); 41 | while (m--) { 42 | int u, v; 43 | cin >> u >> v; 44 | adj[u].push_back(v); 45 | } 46 | int c = 0; 47 | while (l--) { 48 | int x; cin >> x; 49 | c+= dfs(x); 50 | } 51 | cout << c; 52 | cout << endl; 53 | } 54 | } -------------------------------------------------------------------------------- /DPproblems/41-FibonacciSeries.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | #define endl "\n" 6 | #define oo 0x3f3f3f3fLL 7 | #define OO (oo*oo) 8 | #define sz(s) (int)(s.size()) 9 | #define RT(s) return cout<> n; 47 | cout << fibonacci(n); 48 | } 49 | -------------------------------------------------------------------------------- /UVa/108/10865.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | #define ll long long 5 | #define endl '\n' 6 | #define RT(v) return cout< point; 25 | #define X real() 26 | #define Y imag() 27 | int main() { 28 | run(); 29 | int n; 30 | while (cin >> n, n) { 31 | vector v(n); 32 | point mid; 33 | for (int i = 0; i < n; i++) { 34 | int x, y; cin >> x >> y; 35 | v[i] = { x,y }; 36 | if (i == n / 2) 37 | mid = { x,y }; 38 | } 39 | int cnt[2][2]; 40 | clr(cnt, 0); 41 | for (int i = 0; i < n; i++) { 42 | if (v[i].X == mid.X || v[i].Y == mid.Y) 43 | continue; 44 | cnt[v[i].X > mid.X][v[i].Y > mid.Y]++; 45 | } 46 | cout << cnt[0][0] + cnt[1][1] << ' ' 47 | << cnt[1][0] + cnt[0][1] << endl; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /LiveArchive/8078.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | #define endl "\n" 6 | #define ll long long 7 | #define oo 0x3f3f3f3fLL 8 | #define sz(s) (int)(s.size()) 9 | #define RT(s) return cout<> t; 31 | for (int I = 1; I <= t;I++) { 32 | cout << "Case " << I << ":\n"; 33 | string s; cin >> s; 34 | for (char& ch : s) 35 | ch = string("(){}[]<>").find(ch); 36 | vector ans(sz(s)); 37 | stack st; 38 | for (int i = sz(s) - 1; i >= 0; i--) { 39 | if (st.size() && (s[i] & 1) == 0 && s[i] + 1 == s[st.top()]) { 40 | ans[i] = st.top() - i + 1; 41 | if (st.top() + 1 < sz(s)) 42 | ans[i] += ans[st.top() + 1]; 43 | st.pop(); 44 | } 45 | else st.push(i); 46 | } 47 | for (int it : ans)cout << it << endl; 48 | } 49 | } -------------------------------------------------------------------------------- /spoj/XOINC.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | #define endl "\n" 6 | #define ll long long 7 | #define oo 0x3f3f3f3fLL 8 | #define INF 0x3f3f3f3f3f3f3f3fLL 9 | #define sz(s) (int)(s.size()) 10 | #define RT(s) return cout<> n; 43 | for (int i = 1; i <= n; i++) { 44 | cin >> sum[i]; 45 | sum[i] += sum[i - 1]; 46 | } 47 | cout << solve(1, 2, 1); 48 | } 49 | -------------------------------------------------------------------------------- /UVa/123/12318.cpp: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | #include 4 | using namespace std; 5 | typedef long long ll; 6 | typedef unsigned long long ull; 7 | typedef long double ld; 8 | typedef vector vi; 9 | #define Fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); 10 | #define endl '\n' 11 | #define sz(s) (int)(s.size()) 12 | #define all(s) s.begin(),s.end() 13 | #define allr(s) s.rbegin(),s.rend() 14 | #define RT(s) return cout<> 1, mod) % mod; 27 | return (((r * r) % mod) * power(x, y & 1, mod)) % mod; 28 | } 29 | int main() { 30 | Fast; 31 | file(); 32 | ll n, m, k; 33 | while (cin >> n >> m,(n||m)) { 34 | n++; 35 | cin >> k; 36 | vector v(k + 1); 37 | for (int i = 0; i <= k; i++) { 38 | cin >> v[i]; v[i] %= n; 39 | } 40 | set st; 41 | for (int i = 0; i <= m; i++) { 42 | ll sum = 0; 43 | for (int j = 0; j <= k; j++) { 44 | sum = (sum + (v[j] * power(i, j, n)) % n) % n; 45 | } 46 | st.insert(sum); 47 | } 48 | cout << sz(st) << endl; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /spoj/WEIRDFN.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | #define ll long long 5 | #define endl '\n' 6 | #define RT(v) return cout<> t; 27 | while (t--) { 28 | priority_queue lq; 29 | priority_queue, greater> gq; 30 | lq.push(1); 31 | ll n, a, b, c; 32 | cin >> a >> b >> c >> n; 33 | ll sum = 1; 34 | for (int i = 2; i <= n; i++) { 35 | int md = lq.top(); 36 | int cur = (((md * a) % mod) + ((i * b) % mod) + c) % mod; 37 | sum += cur; 38 | lq.push(cur); 39 | while (sz(lq) > sz(gq)) { 40 | cur = lq.top(); lq.pop(); 41 | gq.push(cur); 42 | } 43 | if (sz(gq) > sz(lq)) { 44 | cur = gq.top(); gq.pop(); 45 | lq.push(cur); 46 | } 47 | } 48 | cout << sum << endl; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /spoj/TWINSNOW.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | #define endl "\n" 6 | #define ll long long 7 | #define oo 0x3f3f3f3fLL 8 | #define sz(s) (int)(s.size()) 9 | #define RT(s) return cout<> n; 40 | vector> v(n); 41 | set> st; 42 | for (auto &it : v) { 43 | it = vector(6); 44 | for (auto &i : it) 45 | cin >> i; 46 | sort(all(it)); 47 | if(st.find(it) != st.end()) 48 | RT("Twin snowflakes found."); 49 | st.insert(it); 50 | } 51 | RT("No two snowflakes are alike."); 52 | } 53 | -------------------------------------------------------------------------------- /UVa/006/668.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | #define ll long long 5 | #define endl '\n' 6 | #define RT(v) return cout< rem)return -oo; 27 | int& rt = mem[cur][rem]; 28 | if (~rt)return rt; 29 | rt = solve(cur + 1, rem); 30 | rt = max(rt, 1 + solve(cur + 1, rem - cur)); 31 | return rt; 32 | } 33 | 34 | void build(int cur, int rem) { 35 | if (rem == 0 || rem < 0 || cur > rem)return; 36 | if (solve(cur,rem) == solve(cur + 1, rem)) 37 | build(cur + 1, rem); 38 | else { 39 | cout << cur << " \n"[rem - cur == 0]; 40 | build(cur + 1, rem - cur); 41 | } 42 | } 43 | int main() { 44 | run(); 45 | clr(mem, -1); 46 | int t; cin >> t; 47 | while (t--) { 48 | int n; cin >> n; 49 | build(2, n); 50 | if (t)cout << endl; 51 | } 52 | } -------------------------------------------------------------------------------- /UVa/013/1362.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | #define ll long long 6 | #define endl '\n' 7 | #define RT(v) return cout<= mod)a -= mod; 28 | } 29 | 30 | int multiply(int a, int b) { 31 | return (1ll * a * b) % mod; 32 | } 33 | 34 | int mem[309][309]; 35 | int solve(int l, int r) { 36 | int& rt = mem[l][r]; 37 | if (~rt)return rt; 38 | if (l == r)return rt = 1; 39 | if (s[l] != s[r])return rt = 0; 40 | rt = 0; 41 | for (int k = l + 2; k <= r; k++) 42 | if (s[l] == s[k]) 43 | add(rt, multiply(solve(l + 1, k - 1), solve(k, r))); 44 | return rt; 45 | } 46 | 47 | int main() { 48 | run(); 49 | while (cin >> s) { 50 | clr(mem, -1); 51 | cout << solve(0, sz(s)-1) << endl; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /UVa/011/1112.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | #define ll long long 5 | #define endl '\n' 6 | #define RT(v) return cout<> t; 26 | while (t--) { 27 | int n, e, ti, m; 28 | cin >> n >> e >> ti >> m; 29 | vector> adj(n + 1, vector(n + 1, (ll)oo * oo)); 30 | for (int i = 1; i <= n; i++)adj[i][i] = 0; 31 | while (m--) { 32 | int u, v; 33 | ll w; 34 | cin >> u >> v >> w; 35 | adj[u][v] = min(adj[u][v], w); 36 | } 37 | for (int k = 1; k <= n; k++) 38 | for (int i = 1; i <= n; i++) 39 | for (int j = 1; j <= n; j++) 40 | adj[i][j] = min(adj[i][j], adj[i][k] + adj[k][j]); 41 | int cnt = 0; 42 | for (int i = 1; i <= n; i++) 43 | cnt += adj[i][e] <= ti; 44 | cout << cnt << endl; 45 | if (t) cout << endl; 46 | } 47 | } -------------------------------------------------------------------------------- /newcomers/difference between subarray and subsequence.md: -------------------------------------------------------------------------------- 1 | # Subarray 2 | 3 | A subarray is contiguous part of array (consecutive positions). 4 | for example the subarrays of array {1,2,3} are {1},{1,2},{1,2,3},{2},{2,3},and {3}. 5 | 6 | for any array of size n, there n\*(n+1)/2 non-empty subarray. 7 | 8 | # How to generate all subarrays? 9 | 10 | We can run three nested loops, the first loop picks starting element , second loop picked the ending element of subarray, and the third loop we can print the subarray or do any thing on it. 11 | 12 | ```cpp 13 | int arr[100]; 14 | int main() { 15 | int n; 16 | cin >> n; 17 | for (int i = 0; i < n; i++) 18 | cin >> arr[i]; 19 | for (int start = 0; start < n; start++) { 20 | for (int last = start; last < n; last++) { 21 | cout << "subarray from " << start << " to " << last << " : "; 22 | for (int k = start; k <= last; k++) 23 | cout << arr[k] << " "; 24 | cout << endl; 25 | } 26 | } 27 | } 28 | ``` 29 | 30 | # Subsequence 31 | 32 | A subsequence is a sequence that can be derived from another sequence by deleting zero or more elements, without changing the order of the remaining elements. 33 | for example subsequence of the sequence {1,2,3,4} are {1}, {2},{3},{4},{1,2},{1,3},{1,4},{2,3},{2,4},{3,4},{1,2,3},{1,2,4},{1,3,4},{2,3,4},{1,2,3,4}. 34 | 35 | More generally, we can say that for a sequence of size n, we have ($2^n$ - 1) non-empty sub-sequences in total. 36 | -------------------------------------------------------------------------------- /DPproblems/20-MaximumProductCutting.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | #define endl "\n" 6 | #define oo 0x3f3f3f3fLL 7 | #define OO (oo*oo) 8 | #define sz(s) (int)(s.size()) 9 | #define RT(s) return cout<> n; 48 | cout << maxProduct(n); 49 | } 50 | -------------------------------------------------------------------------------- /leetCode/palindrome-partitioning.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string s; 4 | vector> palindrome; 5 | vector>> build; 6 | int isPalindrome(int left, int right) { 7 | if (left > right)return 0; 8 | if (left == right || left + 1 == right) 9 | return s[left] == s[right]; 10 | int& rt = palindrome[left][right]; 11 | if (~rt)return rt; 12 | rt = 0; 13 | if (s[left] == s[right]) 14 | rt = isPalindrome(left + 1, right - 1); 15 | return rt; 16 | } 17 | void palindromePartition_allSolution(int start) { 18 | if (start == (int)(s.size()))return; 19 | vector>& rt = build[start]; 20 | if ((int)(rt.size()))return; 21 | string cur = ""; 22 | for (int end = start; end < (int)(s.size()); end++) { 23 | cur.push_back(s[end]); 24 | if (isPalindrome(start, end)) { 25 | if (end + 1 == s.size()) 26 | rt.push_back(vector(1, cur)); 27 | else { 28 | palindromePartition_allSolution(end + 1); 29 | for (auto& it : build[end + 1]) { 30 | rt.push_back(vector(1, cur)); 31 | rt.back().insert(rt.back().end(), it.begin(), it.end()); 32 | } 33 | } 34 | } 35 | } 36 | } 37 | vector> partition(string s) { 38 | this->s = s; 39 | palindrome = vector>(s.size(), vector(s.size(), -1)); 40 | build.resize(s.size() + 1); 41 | palindromePartition_allSolution(0); 42 | return build[0]; 43 | } 44 | }; 45 | -------------------------------------------------------------------------------- /TIMUS/1017.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | #define endl "\n" 6 | #define oo 0x3f3f3f3fLL 7 | #define OO (oo*oo) 8 | #define sz(s) (int)(s.size()) 9 | #define RT(s) return cout< n || n < 0)return 0; 39 | ll& rt = mem[n][last]; 40 | if (~rt)return rt; 41 | rt = solve(n, last + 1); 42 | rt += solve(n - last, last + 1); 43 | return rt; 44 | } 45 | int main() { 46 | run(); 47 | clr(mem, -1); 48 | int n; 49 | cin >> n; 50 | cout << solve(n, 1) - 1 << endl; 51 | } 52 | -------------------------------------------------------------------------------- /UVa/006/624.cpp: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | #include 4 | using namespace std; 5 | typedef long long ll; 6 | typedef unsigned long long ull; 7 | #define endl '\n' 8 | #define clr(v,val) memset(v,val,sizeof(v)) 9 | #define sz(v) (int)(v.size()) 10 | #define all(v) v.begin(),v.end() 11 | #define allr(v) v.rbegin(),v.rend() 12 | #define RT(v,val) return cout< v; 25 | vector out; 26 | int mx = 0; 27 | void maxTrack(int ind, int sum,vector st) { 28 | if (sum > m)return; 29 | if (ind >= sz(v)) { 30 | if (sum > mx) mx = sum, out = st; 31 | if (sum == mx && sz(st) > sz(out))out = st; 32 | return; 33 | } 34 | maxTrack(ind + 1, sum, st); 35 | st.push_back(v[ind]); 36 | maxTrack(ind + 1, v[ind] + sum, st); 37 | } 38 | int main() { 39 | run(); 40 | int n; 41 | while (cin >> m >> n) { 42 | v.resize(n); 43 | out.clear(); 44 | mx = 0; 45 | for (int i = 0; i < n; i++) 46 | cin >> v[i]; 47 | maxTrack(0, 0, {}); 48 | for (int x : out) 49 | cout << x << ' '; 50 | cout << "sum:" << mx << endl; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /UVa/015/1555.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | #define ll long long 5 | #define endl '\n' 6 | #define RT(v) return cout<= 0) ed = md - EPS, ans = md; 44 | else st = md + EPS; 45 | } 46 | return f(ans); 47 | } 48 | 49 | int main() { 50 | run(); 51 | cout << fixed << setprecision(2); 52 | while (cin >> n >> a) { 53 | cout << bs() << endl; 54 | } 55 | } -------------------------------------------------------------------------------- /UVa/103/10394.cpp: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | #include 4 | using namespace std; 5 | typedef long long ll; 6 | typedef unsigned long long ull; 7 | typedef long double ld; 8 | typedef vector vi; 9 | #define Fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); 10 | #define endl '\n' 11 | #define sz(s) (int)(s.size()) 12 | #define all(s) s.begin(),s.end() 13 | #define allr(s) s.rbegin(),s.rend() 14 | #define RT(s) return cout< prime(N + 1, true); 24 | vector Sieve(int n = N) { 25 | prime[0] = prime[1] = false; 26 | for (int i = 4; i <= n; i += 2) prime[i] = false; 27 | for (int i = 3; i * i <= n; i += 2) { 28 | if (prime[i]) 29 | for (int j = 3 * i; j <= n; j += i + i) 30 | prime[j] = false; 31 | } 32 | return prime; 33 | } 34 | 35 | int main() { 36 | Fast; 37 | file(); 38 | Sieve(); 39 | vector> v; 40 | for (int i = 3; i < 20000000; i += 2) { 41 | if (!prime[i + 2])i += 2; 42 | else if (prime[i]) { 43 | v.push_back({ i,i + 2 }); 44 | } 45 | } 46 | int n; 47 | while (cin >> n) { 48 | cout << "(" << v[n - 1].first << ", " << 49 | v[n - 1].second << ")" << endl; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /newcomers/basics math algorithms.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | unsigned long long nCr(int n, int r) { 5 | if (r > n) 6 | return 0; 7 | r = max(r, n - r); //nCr(n,r) = nCr(n,n-r) 8 | unsigned long long ans = 1, div = 1, i = r + 1; 9 | while (i <= n) { 10 | ans *= i; 11 | i++; 12 | ans /= div; 13 | div++; 14 | } 15 | return ans; 16 | } 17 | 18 | unsigned long long nPr(int n, int r) { 19 | if (r > n) 20 | return 0; 21 | unsigned long long p = 1, i = n - r + 1; 22 | while (i <= n) 23 | p *= i++; 24 | return p; 25 | } 26 | 27 | long long gcd(long long a, long long b) { 28 | if (a < b) 29 | swap(a, b); 30 | while (a != 0 && b != 0) { 31 | long long r = a % b; 32 | a = b; 33 | b = r; 34 | } 35 | return a; 36 | } 37 | 38 | long long lcm(long long a, long long b) { 39 | return a / gcd(a, b) * b; 40 | } 41 | 42 | void prime_factors(int n) { 43 | for (int i = 2; i <= n; i++) { 44 | if (n % i == 0) { 45 | int cnt = 0; 46 | while (n % i == 0) { 47 | n /= i; 48 | cnt++; 49 | } 50 | cout << i << ' ' << cnt << endl; 51 | } 52 | } 53 | } 54 | 55 | void prime_factors_sqrt(int n) { 56 | for (int i = 2; i * i <= n; i++) { 57 | if (n % i == 0) { 58 | int cnt = 0; 59 | while (n % i == 0) { 60 | n /= i; 61 | cnt++; 62 | } 63 | cout << i << ' ' << cnt << endl; 64 | } 65 | } 66 | if (n > 1) { 67 | cout << n << ' ' << 1 << endl; 68 | } 69 | } 70 | 71 | int main() { 72 | } 73 | -------------------------------------------------------------------------------- /UVa/101/10115.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | #define ll long long 5 | #define endl '\n' 6 | #define RT(v) return cout<> n, n) { 38 | cin.ignore(); 39 | vector> v(n); 40 | for (int i = 0; i < n; i++) { 41 | getline(cin, v[i].first); 42 | getline(cin, v[i].second); 43 | } 44 | getline(cin, s); 45 | for (int j = 0; j < n; j++) { 46 | int i = 0; 47 | while (i < sz(s)) { 48 | if (find(i, v[j].first)) 49 | replace(i, sz(v[j].first), v[j].second); 50 | else i++; 51 | } 52 | } 53 | cout << s << endl; 54 | } 55 | } -------------------------------------------------------------------------------- /spoj/BYTESM2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | #define ll long long 6 | #define endl '\n' 7 | #define RT(v) return cout<> v; 25 | int mem[109][109]; 26 | int solve(int i, int j) { 27 | if (i == sz(v))return 0; 28 | if (j == sz(v[0]) || j < 0)return -oo; 29 | int& rt = mem[i][j]; 30 | if (~rt)return rt; 31 | rt = solve(i + 1, j - 1); 32 | rt = max(rt, solve(i + 1, j)); 33 | rt = max(rt, solve(i + 1, j + 1)); 34 | rt += v[i][j]; 35 | return rt; 36 | } 37 | 38 | int main() { 39 | run(); 40 | int t; cin >> t; 41 | while (t--) { 42 | clr(mem, -1); 43 | int n, m; 44 | cin >> n >> m; 45 | v = vector>(n + 1, vector(m)); 46 | for (int i = 0; i < n; i++) 47 | for (int j = 0; j < m; j++) 48 | cin >> v[i][j]; 49 | int mx = 0; 50 | for (int i = 0; i < m; i++)mx = max(mx, solve(0, i)); 51 | cout << mx << endl; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /DPproblems/37-CountAs.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | #define endl "\n" 6 | #define oo 0x3f3f3f3fLL 7 | #define OO (oo*oo) 8 | #define sz(s) (int)(s.size()) 9 | #define RT(s) return cout< 0; i++) 41 | rt = max(rt, (i + 1) * solve(n - i - 2)); 42 | return rt; 43 | } 44 | 45 | int main() { 46 | run(); 47 | clr(mem, -1); 48 | int t; cin >> t; 49 | while (t--) { 50 | int n; cin >> n; 51 | cout << solve(n) << endl; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /UVa/101/10192.cpp: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | #include 4 | using namespace std; 5 | typedef long long ll; 6 | typedef unsigned long long ull; 7 | #define endl '\n' 8 | #define clr(v,val) memset(v,val,sizeof(v)) 9 | #define sz(v) (int)(v.size()) 10 | #define all(v) v.begin(),v.end() 11 | #define allr(v) v.rbegin(),v.rend() 12 | #define RT(v,val) return cout<, int> mp; 26 | bool valid(int i, int j) { return i < sz(x) && j < sz(y); } 27 | int mxSub(int i = 0, int j = 0) { 28 | if (!valid(i, j))return 0; 29 | if (mp.find({ i,j }) != mp.end()) return mp[{i, j}]; 30 | if (x[i] == y[j]) 31 | return mp[{i, j}] = 1 + mxSub(i + 1, j + 1); 32 | int mx = mxSub(i + 1, j); 33 | mx = max(mx, mxSub(i, j + 1)); 34 | return mp[{i, j}] = mx; 35 | } 36 | 37 | int main() { 38 | run(); 39 | int I = 0; 40 | while (getline(cin, x)) { 41 | if (x == "#")return 0; 42 | mp.clear(); 43 | cout << "Case #" << ++I << 44 | ": you can visit at most "; 45 | getline(cin, y); 46 | cout << mxSub() << " cities." << endl; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /UVa/100/10056.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | #define endl "\n" 6 | #define ll long long 7 | #define oo 0x3f3f3f3fLL 8 | #define sz(s) (int)(s.size()) 9 | #define RT(s) return cout<> t; 41 | while (t--) { 42 | int n, I; 43 | double p; 44 | cin >> n >> p >> I; 45 | if (p < 1e-8) { 46 | cout << 0.0 << endl; 47 | continue; 48 | } 49 | double res = 0; 50 | double cycle = pow(1 - p, n); 51 | double cur = pow(1 - p, I - 1); 52 | while (cur > 1e-6) { 53 | res += cur * p; 54 | cur *= cycle; 55 | } 56 | cout << res << endl; 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /UVa/127/12748.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | #define ll long long 5 | #define endl '\n' 6 | #define RT(v) return cout< point; 24 | #define X real() 25 | #define Y imag() 26 | #define vec(a,b) (b - a) 27 | #define length(a) (hypot(a.Y,a.X)) 28 | int main() { 29 | run(); 30 | int t; cin >> t; 31 | for (int I = 1; I <= t;I++) { 32 | cout << "Case " << I << ":\n"; 33 | int n, m; cin >> n >> m; 34 | vector> v(n); 35 | for (int i = 0; i < n; i++) { 36 | double x, y, r; cin >> x >> y >> r; 37 | v[i] = { r,point(x,y) }; 38 | } 39 | while (m--) { 40 | double x, y; cin >> x >> y; 41 | point p(x, y); 42 | bool b = 1; 43 | for (int i = 0;b && i < n; i++) { 44 | double d = length(vec(v[i].second, p)); 45 | if (d <= v[i].first) 46 | b = 0; 47 | } 48 | cout << (!b ? "Yes" : "No") << endl; 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /spoj/BLOPER.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | #define ll long long 5 | #define endl '\n' 6 | #define RT(v) return cout< v; 24 | bool build(int n, int rem) { 25 | for (int i = n; i > 1; i--) { 26 | if (rem == i) { 27 | v[rem] = 1; rem = 0; break; 28 | } 29 | else if (rem - i > 1) { 30 | v[i] = 1; rem -= i; 31 | } 32 | } 33 | return (rem == 0); 34 | } 35 | 36 | bool solve(int n, int s) { 37 | int m = n * (n + 1) / 2; 38 | if (s < 0 && abs(s) >= m)RT("Impossible"); 39 | else if(s > m)RT("Impossible"); 40 | if ((m - s) % 2 != 0)RT("Impossible"); 41 | int ne = m - s >> 1; 42 | v = vector(n + 1); 43 | if (!build(n, ne))RT("Impossible"); 44 | cout << 1; 45 | for (int i = 2, j = 0; i <= n; i++) { 46 | if (v[i])cout << '-'; 47 | else cout << '+'; 48 | cout << i; 49 | } 50 | return true; 51 | } 52 | 53 | int main() { 54 | run(); 55 | int n, s; 56 | cin >> n >> s; 57 | solve(n, s); 58 | } 59 | -------------------------------------------------------------------------------- /leetCode/word-break-ii.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | set dictionary; 4 | vector> build; 5 | string word; 6 | vector mem; 7 | int canSplit(int start) { 8 | if (start == word.size())return 1; 9 | int& rt = mem[start]; 10 | if (~rt)return rt; 11 | rt = 0; 12 | string subWord; 13 | for (int end = start; end < word.size(); end++) { 14 | subWord += word[end]; 15 | if (dictionary.find(subWord) != dictionary.end()) 16 | rt |= canSplit(end + 1); 17 | } 18 | return rt; 19 | } 20 | 21 | void breakWord(int start = 0) { 22 | if (start == word.size())return; 23 | vector& rt = build[start]; 24 | if (!rt.empty())return; 25 | string subWord; 26 | for (int end = start; end < word.size(); end++) { 27 | subWord += word[end]; 28 | if (dictionary.find(subWord) != dictionary.end() && canSplit(end + 1)) { 29 | if (end + 1 < word.size()) { 30 | breakWord(end + 1); 31 | vector tmp = build[end + 1]; 32 | for (auto& it : tmp) it = subWord + ' ' + it; 33 | rt.insert(rt.end(), tmp.begin(), tmp.end()); 34 | } 35 | else rt.push_back(subWord); 36 | } 37 | } 38 | } 39 | 40 | vector wordBreak(string s, vector& wordDict) { 41 | word = s; 42 | dictionary = set(wordDict.begin(), wordDict.end()); 43 | mem = vector(word.size() + 1, -1); 44 | build = vector>(word.size() + 1); 45 | build[word.size()] = { "" }; 46 | breakWord(); 47 | return build[0]; 48 | } 49 | }; 50 | -------------------------------------------------------------------------------- /UVa/103/10305.cpp: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | #include 4 | using namespace std; 5 | typedef long long ll; 6 | typedef unsigned long long ull; 7 | #define endl '\n' 8 | #define clr(v,val) memset(v,val,sizeof(v)) 9 | #define sz(v) (int)(v.size()) 10 | #define all(v) v.begin(),v.end() 11 | #define allr(v) v.rbegin(),v.rend() 12 | #define RT(v,val) return cout<> adj; 28 | vector topsort; 29 | bool vis[110]; 30 | 31 | void dfs(int node) { 32 | if (vis[node]) 33 | return; 34 | vis[node] = true; 35 | for (int child : adj[node]) 36 | dfs(child); 37 | topsort.push_back(node); 38 | } 39 | int main() { 40 | run(); 41 | int n, m; 42 | while (cin >> n >> m, (n || m)) { 43 | adj.clear(); 44 | adj.resize(n + 1); 45 | clr(vis, 0); 46 | topsort.clear(); 47 | while (m--) { 48 | int x, y; 49 | cin >> x >> y; 50 | adj[y].push_back(x); 51 | } 52 | for (int i = 1; i <= n; i++) { 53 | dfs(i); 54 | } 55 | cout << topsort[0]; 56 | for (int i = 1; i < n; i++) 57 | cout << ' ' << topsort[i]; 58 | cout << endl; 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /DPproblems/35-UglyNumbers.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | #define endl "\n" 6 | #define oo 0x3f3f3f3fLL 7 | #define OO (oo*oo) 8 | #define sz(s) (int)(s.size()) 9 | #define RT(s) return cout< primes = { 2,3,5 }; 39 | set st; 40 | st.insert(1); 41 | int idx = 0; ll ugly = 0; 42 | while (idx < MAX) { 43 | ugly = *st.begin(); st.erase(st.begin()); 44 | mem[idx++] = ugly; 45 | for (auto it : primes) 46 | st.insert(ugly * it); 47 | } 48 | int t; cin >> t; 49 | while (t--) { 50 | int n; cin >> n; 51 | cout << mem[n - 1] << endl; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /UVa/106/10684.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | #define endl "\n" 6 | #define oo 0x3f3f3f3fLL 7 | #define OO (oo*oo) 8 | #define sz(s) (int)(s.size()) 9 | #define RT(s) return cout<> n, n) { 39 | vector v(n); 40 | for (int i = 0; i < n; i++) { 41 | cin >> v[i]; 42 | if (i)v[i] += v[i - 1]; 43 | } 44 | set st{ 0 }; 45 | int mx = -oo; 46 | for (int i = 0; i < n; i++) { 47 | mx = max(mx, v[i] - *st.begin()); 48 | st.insert(v[i]); 49 | } 50 | if (mx <= 0)cout << "Losing streak.\n"; 51 | else cout << "The maximum winning streak is " << mx << ".\n"; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /UVa/104/10474.cpp: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | #include 4 | using namespace std; 5 | typedef long long ll; 6 | typedef unsigned long long ull; 7 | typedef long double ld; 8 | typedef vector vi; 9 | #define Fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); 10 | #define endl '\n' 11 | #define sz(s) (int)(s.size()) 12 | #define all(s) s.begin(),s.end() 13 | #define allr(s) s.rbegin(),s.rend() 14 | #define RT(s,v) return cout< v; 22 | int binarySearch(int s, int e, int x) { 23 | if (s > e) return -1; 24 | if (s == e) { 25 | if (v[s] == x) return s; 26 | return -1; 27 | } 28 | int m = (s + e) / 2; 29 | if (v[m] < x) return binarySearch(++m, e, x); 30 | if (v[m] > x) return binarySearch(s, --m, x); 31 | if(v[m] == x) return binarySearch(s, m, x); 32 | } 33 | int main() { 34 | Fast; 35 | file(); 36 | int n, q; 37 | int I = 0; 38 | while (cin >> n >> q, n || q) { 39 | v.clear(); 40 | cout << "CASE# " << ++I << ":" << endl; 41 | while (n--) { 42 | int a; cin >> a; 43 | v.push_back(a); 44 | } 45 | sort(all(v)); 46 | while (q--) { 47 | int x; cin >> x; 48 | int ind = binarySearch(0, sz(v) - 1, x); 49 | cout << x << ' '; 50 | if (ind == -1) cout << "not found" << endl; 51 | else cout << "found at " << ++ind << endl; 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /spoj/CHOCOLA.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | #define ll long long 5 | #define endl '\n' 6 | #define RT(v) return cout< x, y; 24 | int mem[1009][1009]; 25 | int solve(int i, int j) { 26 | if (i + 1 == n && j + 1 == m)return 0; 27 | int& rt = mem[i][j]; 28 | if (~rt)return rt; 29 | rt = oo; 30 | if (j + 1 < m) rt = min(rt, x[i] + y[j + 1] - y[j] + solve(i, j + 1)); 31 | if (i + 1 < n) rt = min(rt, y[j] + x[i + 1] - x[i] + solve(i + 1, j)); 32 | return rt; 33 | } 34 | 35 | void init(vector& v) { 36 | sort(v.begin() + 1, v.end()); 37 | for (int i = 1; i < sz(v); i++) 38 | v[i] += v[i - 1]; 39 | } 40 | int main() { 41 | run(); 42 | int t; cin >> t; 43 | while (t--) { 44 | clr(mem, -1); 45 | cin >> n >> m; 46 | x = vector(n); 47 | y = vector(m); 48 | for (int i = 1; i < n; i++)cin >> x[i]; 49 | for (int i = 1; i < m; i++)cin >> y[i]; 50 | init(x); init(y); 51 | cout << solve(0, 0) << endl; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /UVa/100/10004.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | #define ll long long 5 | #define endl '\n' 6 | #define RT(v) return cout< vis; 24 | vector> adj; 25 | vector cl; 26 | bool b; 27 | void dfs(int node, bool c) { 28 | vis[node] = 1; 29 | cl[node] = c; 30 | for (int child : adj[node]) 31 | if (cl[child] == -1) 32 | dfs(child, !c); 33 | else if (cl[child] == cl[node]) { 34 | b = 0; return; 35 | } 36 | } 37 | 38 | int main() { 39 | run(); 40 | int n, m; 41 | while (cin >> n >> m) { 42 | b = 1; 43 | adj = vector>(n); 44 | cl = vector(n, -1); 45 | vis = vector(n); 46 | while (m--) { 47 | int u, v; 48 | cin >> u >> v; 49 | adj[u].push_back(v); 50 | adj[v].push_back(u); 51 | } 52 | for (int i = 0; i < n; i++) 53 | if (!vis[i]) dfs(i, 1); 54 | if (b)cout << "BICOLORABLE."; 55 | else cout << "NOT BICOLORABLE."; 56 | cout << endl; 57 | } 58 | } -------------------------------------------------------------------------------- /UVa/106/10626.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | #define ll long long 6 | #define endl '\n' 7 | #define RT(v) return cout<, int> mem[160]; 25 | void Min(int& a, int b) { a = min(a, b); } 26 | int solve(int c, int n1, int n5, int n10) { 27 | if (n10 < 0 || n5 < 0 || n1 < 0)return oo; 28 | if (c == 0)return 0; 29 | if (mem[c].count({ n1,n5,n10 }))return mem[c][{n1, n5, n10}]; 30 | int& rt = mem[c][{n1, n5, n10}]; 31 | rt = oo; 32 | Min(rt, 1 + solve(c - 1, n1 + 2, n5 , n10 - 1)); 33 | Min(rt, 4 + solve(c - 1, n1 - 3, n5 + 1, n10 - 1)); 34 | Min(rt, 4 + solve(c - 1, n1 - 3, n5 - 1, n10)); 35 | Min(rt, 2 + solve(c - 1, n1 + 2, n5 - 2, n10)); 36 | Min(rt, 8 + solve(c - 1, n1 - 8, n5 , n10)); 37 | return rt; 38 | } 39 | 40 | int main() { 41 | run(); 42 | int t; cin >> t; 43 | while (t--) { 44 | int c, n1, n5, n10; 45 | cin >> c >> n1 >> n5 >> n10; 46 | cout << solve(c, n1, n5, n10) << endl; 47 | } 48 | } -------------------------------------------------------------------------------- /UVa/117/11703.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | #define endl "\n" 6 | #define oo 0x3f3f3f3fLL 7 | #define OO (oo*oo) 8 | #define sz(s) (int)(s.size()) 9 | #define RT(s) return cout<= 1000000)a -= 1000000; 38 | } 39 | int x(int i) { 40 | if (i == 0)return 1; 41 | int& rt = mem[i]; 42 | if (~rt)return rt; 43 | rt = 0; 44 | add(rt, x(i - sqrt(i))); 45 | add(rt, x(log(i))); 46 | add(rt, x(i * sin(i) * sin(i))); 47 | return rt; 48 | } 49 | int main() { 50 | run(); 51 | clr(mem, -1); 52 | int n; 53 | while (cin >> n, n != -1) { 54 | cout << x(n) << endl; 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /UVa/102/10242.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | #define ll long long 5 | #define endl '\n' 6 | #define RT(v) return cout< point; 25 | #define X real() 26 | #define Y imag() 27 | #define vec(a,b) (b - a) 28 | #define length(a) (hypot(a.Y,a.X)) 29 | int main() { 30 | run(); 31 | cout << fixed << setprecision(3); 32 | point p[4]; 33 | double x, y; 34 | while (cin >> x >> y) { 35 | p[0] = point(x, y); 36 | for (int i = 1; i < 4; i++) { 37 | cin >> x >> y; 38 | p[i] = point(x, y); 39 | } 40 | vector v; 41 | for (int i = 0; i < 4; i++) { 42 | for (int j = i + 1; j < 4; j++) { 43 | if (p[i] == p[j]) { 44 | for (int k = 0; k < 4; k++) { 45 | if (k == i || k == j)continue; 46 | v.push_back(k); 47 | } 48 | v.push_back(i); 49 | } 50 | } 51 | } 52 | point ans = p[v[0]] + p[v[1]] - p[v[2]]; 53 | cout << ans.X << ' ' << ans.Y << endl; 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /spoj/MAKETREE.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | typedef long long ll; 5 | typedef unsigned long long ull; 6 | #define endl '\n' 7 | #define clr(v,val) memset(v,val,sizeof(v)) 8 | #define sz(v) (int)(v.size()) 9 | #define all(v) v.begin(),v.end() 10 | #define RT(v) return cout<> adj; 25 | vector vis; 26 | vector topsort; 27 | void dfs(int node) { 28 | if (vis[node])return; 29 | vis[node] = true; 30 | for (int child : adj[node]) 31 | dfs(child); 32 | topsort.push_back(node); 33 | } 34 | int main() { 35 | run(); 36 | int n, k; 37 | cin >> n >> k; 38 | adj.resize(n + 1); vis.resize(n + 1); 39 | for (int i = 1; i <= k; i++) { 40 | int m; cin >> m; 41 | while (m--) { 42 | int x; cin >> x; 43 | adj[i].push_back(x); 44 | } 45 | } 46 | for (int i = 1; i <= n; i++) dfs(i); 47 | topsort.push_back(0); 48 | reverse(all(topsort)); 49 | vector par(n + 1); 50 | for (int i = 1; i <= n; i++) { 51 | par[topsort[i]] = topsort[i - 1]; 52 | } 53 | for (int i = 1; i <= n; i++) 54 | cout << par[i] << endl; 55 | } 56 | -------------------------------------------------------------------------------- /UVa/114/11495.cpp: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | #include 4 | using namespace std; 5 | typedef long long ll; 6 | typedef unsigned long long ull; 7 | typedef long double ld; 8 | typedef vector vi; 9 | #define Fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); 10 | #define endl '\n' 11 | #define sz(s) (int)(s.size()) 12 | #define all(s) s.begin(),s.end() 13 | #define allr(s) s.rbegin(),s.rend() 14 | #define RT(s,v) return cout< temp; 23 | void merge(vi& v, int s, int m, int e) { 24 | for (int i = s; i <= e; i++) temp[i] = v[i]; 25 | int i = s, j = m + 1, k = s; 26 | while (i <= m && j <= e) 27 | if (temp[i] <= temp[j]) v[k++] = temp[i++]; 28 | else v[k++] = temp[j++], cnt += j - k; 29 | while (i <= m)v[k++] = temp[i++]; 30 | while (j <= e)v[k++] = temp[j++]; 31 | } 32 | 33 | void merge_sort(vi & v, int s, int e) { 34 | if (s >= e) return; 35 | int m = (s + e) / 2; 36 | merge_sort(v, s, m); 37 | merge_sort(v, m + 1, e); 38 | merge(v, s, m, e); 39 | } 40 | 41 | int main() { 42 | Fast; 43 | file(); 44 | int n; 45 | while (cin >> n, n) { 46 | temp.resize(n); 47 | vector v(n); 48 | for (int i = 0; i < n; i++) 49 | cin >> v[i]; 50 | cnt = 0; 51 | merge_sort(v, 0, n - 1); 52 | cout << ((cnt & 1) ? "Marcelo" : "Carlos") << endl; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /UVa/115/11520.cpp: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | #include 4 | using namespace std; 5 | typedef long long ll; 6 | typedef unsigned long long ull; 7 | typedef long double ld; 8 | typedef vector vi; 9 | #define Fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); 10 | #define endl '\n' 11 | #define sz(s) (int)(s.size()) 12 | #define all(s) s.begin(),s.end() 13 | #define allr(s) s.rbegin(),s.rend() 14 | #define RT(s,v) return cout<> t; 25 | int I = 0; 26 | while (I < t) { 27 | cout << "Case " << ++I << ":\n"; 28 | int n; cin >> n; 29 | char arr[12][12]; 30 | for (int i = 0; i < n; i++) 31 | for (int j = 0; j < n; j++) 32 | cin >> arr[i][j]; 33 | for (int i = 0; i < n; i++) { 34 | for (int j = 0; j < n; j++) { 35 | if (arr[i][j] == '.') { 36 | for (char ch = 'A'; ch <= 'Z'; ch++) { 37 | if (i + 1 < n && arr[i + 1][j] == ch 38 | || j + 1 < n && arr[i][j + 1] == ch 39 | || i - 1 >= 0 && arr[i - 1][j] == ch 40 | || j - 1 >= 0 && arr[i][j - 1] == ch) 41 | continue; 42 | arr[i][j] = ch; 43 | break; 44 | } 45 | } 46 | } 47 | } 48 | for (int i = 0; i < n; i++) { 49 | for (int j = 0; j < n; j++) 50 | cout << arr[i][j]; 51 | cout << endl; 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /UVa/115/11536.cpp: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | #include 4 | using namespace std; 5 | typedef long long ll; 6 | typedef unsigned long long ull; 7 | typedef long double ld; 8 | typedef vector vi; 9 | #define Fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); 10 | #define endl '\n' 11 | #define sz(s) (int)(s.size()) 12 | #define all(s) s.begin(),s.end() 13 | #define allr(s) s.rbegin(),s.rend() 14 | #define RT(s,v) return cout<> t; 25 | for (int I = 1; I <= t; I++) { 26 | cout << "Case " << I << ": "; 27 | int n, m, k; 28 | cin >> n >> m >> k; 29 | vector v(n); 30 | int i = 0; 31 | while (i < 3)v[i] = i + 1, i++; 32 | while (i < n) { 33 | for (int j = i - 3; j < i; j++) 34 | v[i] += v[j]; 35 | v[i] %= m; 36 | v[i++]++; 37 | } 38 | int l = 0, r = 0, mn = INT_MAX; 39 | map mp; 40 | while (l < n) { 41 | while (r < n) { 42 | if (sz(mp) == k) break; 43 | if (v[r] <= k) mp[v[r]]++; 44 | r++; 45 | } 46 | if (sz(mp) < k) break; 47 | mn = min(mn, r - l); 48 | if (v[l] <= k) { 49 | mp[v[l]]--; 50 | if (mp[v[l]] == 0)mp.erase(v[l]); 51 | } 52 | l++; 53 | } 54 | if (mn == INT_MAX) 55 | cout << "sequence nai"; 56 | else cout << mn; 57 | cout << endl; 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /UVa/002/229.cpp: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | #include 4 | using namespace std; 5 | typedef long long ll; 6 | typedef unsigned long long ull; 7 | typedef long double ld; 8 | typedef vector vi; 9 | #define Fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); 10 | #define endl '\n' 11 | #define sz(s) (int)(s.size()) 12 | #define all(s) s.begin(),s.end() 13 | #define allr(s) s.rbegin(),s.rend() 14 | #define RT(s) return cout< temp; 23 | void merge(vi& v, int s, int m, int e) { 24 | for (int i = s; i <= e; i++) temp[i] = v[i]; 25 | int i = s, j = m + 1, k = s; 26 | while (i <= m && j <= e) 27 | if (temp[i] <= temp[j]) v[k++] = temp[i++]; 28 | else v[k++] = temp[j++], cnt += j - k; 29 | while (i <= m)v[k++] = temp[i++]; 30 | while (j <= e)v[k++] = temp[j++]; 31 | } 32 | 33 | void merge_sort(vi & v, int s, int e) { 34 | if (s >= e) return; 35 | int m = (s + e) / 2; 36 | merge_sort(v, s, m); 37 | merge_sort(v, m + 1, e); 38 | merge(v, s, m, e); 39 | } 40 | int main() { 41 | //Fast; 42 | file(); 43 | int t; cin >> t; 44 | while (t--) { 45 | int n; cin >> n; 46 | cnt = 0; 47 | vector v(n); 48 | temp.resize(n); 49 | for (int i = 0; i < n; i++) cin >> v[i]; 50 | merge_sort(v, 0, n - 1); 51 | printf("Optimal train swapping takes %d swaps.\n", cnt); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /spoj/PT07Z.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | #define endl "\n" 6 | #define ll long long 7 | #define oo 0x3f3f3f3fLL 8 | #define sz(s) (int)(s.size()) 9 | #define RT(s) return cout< adj[N]; 37 | pair p; 38 | void dfs(int node, int par, int dep) { 39 | p = max(p, { dep, node }); 40 | for (int ch : adj[node]) 41 | if (ch != par) 42 | dfs(ch, node, dep + 1); 43 | } 44 | 45 | int main() { 46 | run(); 47 | int n; 48 | cin >> n; 49 | for (int i = 1; i < n; i++) { 50 | int u, v; 51 | cin >> u >> v; 52 | adj[u].push_back(v); 53 | adj[v].push_back(u); 54 | } 55 | dfs(1, -1, 0); 56 | pair tmp = p; 57 | p = { 0, 0 }; 58 | dfs(tmp.second, -1, 0); 59 | cout << p.first; 60 | } -------------------------------------------------------------------------------- /UVa/107/10703.cpp: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | #include 4 | using namespace std; 5 | typedef long long ll; 6 | typedef unsigned long long ull; 7 | typedef long double ld; 8 | typedef vector vi; 9 | #define Fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); 10 | #define endl '\n' 11 | #define sz(s) (int)(s.size()) 12 | #define all(s) s.begin(),s.end() 13 | #define allr(s) s.rbegin(), s.rend() 14 | #define exit(s) return cout<> w >> h >> n) { 27 | if (!w && !h && !n) return 0; 28 | memset(arr, 0, sizeof(arr)); 29 | while (n--) { 30 | int x1, y1, x2, y2; 31 | cin >> x1 >> y1 >> x2 >> y2; 32 | if (x1 > x2) swap(x1, x2); 33 | if (y1 > y2) swap(y1, y2); 34 | arr[x1][y1]++; 35 | arr[x1][y2 + 1]--; 36 | arr[x2 + 1][y1]--; 37 | arr[x2 + 1][y2 + 1]++; 38 | } 39 | for (int i = 1; i <= w; i++) 40 | for (int j = 1; j <= h; j++) 41 | arr[i][j] += arr[i - 1][j] + arr[i][j - 1] - arr[i - 1][j - 1]; 42 | int cnt = 0; 43 | for (int i = 1; i <= w; i++) 44 | for (int j = 1; j <= h; j++) 45 | if (arr[i][j] == 0)cnt++; 46 | if (cnt == 0) cout << "There is no empty spots.\n"; 47 | else if (cnt == 1) cout << "There is one empty spot.\n"; 48 | else printf("There are %d empty spots.\n", cnt); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /UVa/117/11730.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | #define ll long long 5 | #define endl '\n' 6 | #define RT(v) return cout< fac[N]; 24 | void primeFactors() { 25 | for (int i = 2; i < N; i += 2) 26 | fac[i].push_back(2); 27 | for (int i = 3; i < N; i += 2) { 28 | if (fac[i].empty()) for (int j = i; j < N; j += i) 29 | fac[j].push_back(i); 30 | } 31 | } 32 | 33 | void Min(int& a, int b) { a = min(a, b); } 34 | int main() { 35 | run(); 36 | primeFactors(); 37 | vector> dp(101, vector(1001, oo)); 38 | for (int s = 1; s <= 100; s++) { 39 | dp[s][s] = 0; 40 | for (int t = s; t < 1000; t++) { 41 | if (dp[s][t] == oo)continue; 42 | for (auto a : fac[t]) { 43 | if (a == t)continue; 44 | if (t + a > 1000)break; 45 | Min(dp[s][t + a], 1 + dp[s][t]); 46 | } 47 | } 48 | } 49 | int I = 1, s, t; 50 | while (cin >> s >> t, s || t) { 51 | cout << "Case " << I++ << ": "; 52 | cout << (dp[s][t] == oo ? -1 : dp[s][t]) << endl; 53 | } 54 | } -------------------------------------------------------------------------------- /DPproblems/27-NumberOfWaysToScorePoints.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | #define endl "\n" 6 | #define oo 0x3f3f3f3fLL 7 | #define OO (oo*oo) 8 | #define sz(s) (int)(s.size()) 9 | #define RT(s) return cout<> n; 52 | for (int i = 0; i < 3; i++)cin >> scores[i]; 53 | cout << numberOfWaysToScore(n); 54 | } 55 | -------------------------------------------------------------------------------- /spoj/PERMUT1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | #define endl "\n" 6 | #define ll long long 7 | #define sz(s) (int)(s.size()) 8 | #define INF 0x3f3f3f3f3f3f3f3fLL 9 | #define all(v) v.begin(),v.end() 10 | #define watch(x) cout<<(#x)<<" = "<= 201402L 14 | template 15 | vector create(size_t n) { 16 | return vector(n); 17 | } 18 | template 19 | auto create(size_t n, Args ... args) { 20 | return vector(args...))>(n, create(args...)); 21 | } 22 | #endif 23 | void run() { 24 | ios::sync_with_stdio(false); 25 | cin.tie(NULL); 26 | cout.tie(NULL); 27 | #ifdef EZZAT 28 | freopen("input.in", "r", stdin); 29 | //freopen("output.out", "w", stdout); 30 | #else 31 | #endif 32 | } 33 | void solve() { 34 | int n, k; 35 | cin >> n >> k; 36 | vector> dp(1 << n, vector(k + 1)); 37 | dp[0][0] = 1; 38 | for (int mask = 0; mask < (1 << n); mask++) 39 | for (int cnt = 0; cnt <= k; cnt++) { 40 | int cntG = 0; 41 | for (int cur = n - 1; cur >= 0 && cnt + cntG <= k; cur--) { 42 | if (mask & (1 << cur)) { 43 | cntG++; 44 | continue; 45 | } 46 | dp[mask | (1 << cur)][cnt + cntG] += dp[mask][cnt]; 47 | } 48 | 49 | } 50 | cout << dp[(1 << n) - 1][k] << endl; 51 | } 52 | 53 | int main() { 54 | run(); 55 | int T; 56 | cin >> T; 57 | while (T--) { 58 | solve(); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /UVa/117/11753.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | #define ll long long 5 | #define endl '\n' 6 | #define RT(v) return cout< v; 24 | int n, k; 25 | int mem[10001][21][21]; 26 | int solve(int i = 0, int shiftL = 0, int shiftR = 0) { 27 | if (shiftR + shiftL > k)return 21; 28 | int j = n - i - 1 - shiftL + shiftR; 29 | if (j <= i)return 0; 30 | int& rt = mem[i][shiftL][shiftR]; 31 | if (~rt)return rt; 32 | rt = 21; 33 | if (v[i] == v[j]) rt = solve(i + 1, shiftL, shiftR); 34 | rt = min(rt, 1 + solve(i + 1, shiftL, shiftR + 1)); 35 | rt = min(rt, 1 + solve(i, shiftL + 1, shiftR)); 36 | return rt; 37 | } 38 | 39 | int main() { 40 | run(); 41 | int t; cin >> t; 42 | for (int I = 1; I <= t; I++) { 43 | cout << "Case " << I << ": "; 44 | clr(mem, -1); 45 | cin >> n >> k; 46 | v = vector(n); 47 | for (int i = 0; i < n; i++)cin >> v[i]; 48 | if (solve() == 0)cout << "Too easy"; 49 | else if (solve() <= k) cout << solve(); 50 | else cout << "Too difficult"; 51 | cout << endl; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /UVa/124/12484.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | #define endl "\n" 6 | #define oo 0x3f3f3f3fLL 7 | #define OO (oo*oo) 8 | #define sz(s) (int)(s.size()) 9 | #define RT(s) return cout<> n) { 39 | clr(mem, 0); 40 | vector v(n); 41 | for (auto& a : v)cin >> a; 42 | bool cur = 0; 43 | for (int len = 1; len < n; len++) { 44 | cur ^= 1; 45 | for (int i = 0; i + len < n; i++) { 46 | int j = i + len; 47 | if (cur) mem[cur][i] = max(v[i] + mem[cur ^ 1][i + 1], mem[cur ^ 1][i] + v[j]); 48 | else mem[cur][i] = min(mem[cur ^ 1][i], mem[cur ^ 1][i + 1]); 49 | } 50 | } 51 | cout << mem[cur][0] << endl; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /UVa/106/10651.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | #define endl "\n" 6 | #define ll long long 7 | #define sz(s) (int)(s.size()) 8 | #define INF 0x3f3f3f3f3f3f3f3fLL 9 | #define all(v) v.begin(),v.end() 10 | #define watch(x) cout<<(#x)<<" = "<= 201402L 14 | template 15 | vector create(size_t n) { 16 | return vector(n); 17 | } 18 | template 19 | auto create(size_t n, Args ... args) { 20 | return vector(args...))>(n, create(args...)); 21 | } 22 | #endif 23 | void run() { 24 | ios::sync_with_stdio(false); 25 | cin.tie(NULL); 26 | cout.tie(NULL); 27 | #ifdef EZZAT 28 | freopen("input.in", "r", stdin); 29 | //freopen("output.out", "w", stdout); 30 | #else 31 | #endif 32 | } 33 | 34 | const int N = (1 << 12); 35 | int dp[N]; 36 | int solve(int mask) { 37 | int &rt = dp[mask]; 38 | if (~rt) 39 | return rt; 40 | rt = __builtin_popcount(mask); 41 | for (int j = 0; j < 10; j++) { 42 | int AND = ((mask >> j) & 7); 43 | if (AND == 3 || AND == 6) 44 | rt = min(rt, solve(mask ^ (7 << j))); 45 | } 46 | return rt; 47 | } 48 | int main() { 49 | run(); 50 | int T; 51 | cin >> T; 52 | memset(dp, -1, sizeof dp); 53 | while (T--) { 54 | string s; 55 | cin >> s; 56 | int mask = 0; 57 | for (int i = 0; i < sz(s); i++) 58 | if (s[i] == 'o') 59 | mask |= (1 << i); 60 | cout << solve(mask) << endl; 61 | } 62 | } -------------------------------------------------------------------------------- /DPproblems/43-BoxStacking.cpp: -------------------------------------------------------------------------------- 1 | { 2 | #include 3 | using namespace std; 4 | int maxHeight(int height[],int width[],int length[],int n); 5 | int main() 6 | { 7 | int t; 8 | cin>>t; 9 | while(t--){ 10 | int n; 11 | cin>>n; 12 | 13 | int A[1000],B[1000],C[10001]; 14 | for(int i=0;i>a>>b>>c; 18 | A[i]=a; 19 | B[i]=b; 20 | C[i]=c; 21 | } 22 | cout<> v; 33 | int mem[101][101]; 34 | int solve(int d1, int d2) { 35 | int& rt = mem[d1][d2]; 36 | if (~rt)return rt; 37 | rt = 0; 38 | for (int i = 0; i < (int)(v.size()); i++) for (int j = 0; j < 3; j++) { 39 | int nxt1 = (j + 1) % 3, nxt2 = (j + 2) % 3; 40 | if (v[i][nxt1] > d1 && v[i][nxt2] > d2) { 41 | rt = max(rt, v[i][j] + solve(v[i][nxt1], v[i][nxt2])); 42 | } 43 | swap(nxt1, nxt2); 44 | if (v[i][nxt1] > d1 && v[i][nxt2] > d2) { 45 | rt = max(rt, v[i][j] + solve(v[i][nxt1], v[i][nxt2])); 46 | } 47 | } 48 | return rt; 49 | } 50 | 51 | int maxHeight(int height[], int width[], int length[], int n) 52 | { 53 | v = vector>(n, vector(3)); 54 | for (int i = 0; i < n; i++) 55 | v[i] = { height[i],width[i],length[i] }; 56 | memset(mem, -1, sizeof(mem)); 57 | return solve(0, 0); 58 | } 59 | -------------------------------------------------------------------------------- /DPproblems/11-EggDropping.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | #define endl "\n" 6 | #define oo 0x3f3f3f3fLL 7 | #define OO (oo*oo) 8 | #define sz(s) (int)(s.size()) 9 | #define RT(s) return cout<> t; 51 | while (t--) { 52 | int n, k; 53 | cin >> n >> k; 54 | cout << eggDropping(n, k) << endl; 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /spoj/INVCNT.cpp: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | #include 4 | using namespace std; 5 | typedef long long ll; 6 | typedef unsigned long long ull; 7 | typedef long double ld; 8 | typedef vector vi; 9 | #define Fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); 10 | #define endl '\n' 11 | #define sz(s) (int)(s.size()) 12 | #define all(s) s.begin(),s.end() 13 | #define allr(s) s.rbegin(),s.rend() 14 | #define RT(s,v) return cout< temp; 23 | void merge(vi& v, int s, int m, int e) { 24 | for (int i = s; i <= e; i++) temp[i] = v[i]; 25 | int i = s, j = m + 1, k = s; 26 | while (i <= m && j <= e) 27 | if (temp[i] <= temp[j]) v[k++] = temp[i++]; 28 | else v[k++] = temp[j++], cnt += j - k; 29 | while (i <= m)v[k++] = temp[i++]; 30 | while (j <= e)v[k++] = temp[j++]; 31 | } 32 | 33 | void merge_sort(vi & v, int s, int e) { 34 | if (temp.empty())temp.resize(e + 1); 35 | if (s >= e) return; 36 | int m = (s + e) / 2; 37 | merge_sort(v, s, m); 38 | merge_sort(v, m + 1, e); 39 | merge(v, s, m, e); 40 | } 41 | int main() { 42 | Fast; 43 | file(); 44 | int t; cin >> t; 45 | for (int I = 0; I < t;I++) { 46 | int n; cin >> n; 47 | vector v(n); 48 | temp.clear(); 49 | temp.resize(n); 50 | for (int i = 0; i < n; i++)cin >> v[i]; 51 | cnt = 0; 52 | merge_sort(v, 0, n - 1); 53 | cout << cnt< 2 | #include 3 | using namespace std; 4 | #define ll long long 5 | #define endl '\n' 6 | #define RT(v) return cout< point; 25 | #define X real() 26 | #define Y imag() 27 | #define vec(a,b) (b - a) 28 | #define length(a) (hypot(a.Y,a.X)) 29 | int main() { 30 | run(); 31 | int n; 32 | bool b = 0; 33 | while (cin >> n) { 34 | if (b)cout << endl; 35 | else b = 1; 36 | double r = n - 0.5; 37 | pair cnt = { 0, 0 }; 38 | for (int i = 0; i < n; i++) { 39 | for (int j = 0; j < n; j++) { 40 | point p(i, j); 41 | double l1 = length(p); 42 | p = point(i + 1, j + 1); 43 | double l2 = length(p); 44 | if (l1 <= r && l2 <= r) 45 | cnt.second += 4; 46 | else if (l1 <= r && l2 > r) 47 | cnt.first += 4; 48 | } 49 | } 50 | cout << "In the case n = " << n << ", " << cnt.first 51 | << " cells contain segments of the circle.\nThere are " << 52 | cnt.second << " cells completely contained in the circle.\n"; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /DPproblems/9-DiceThrowWays.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | #define endl "\n" 6 | #define oo 0x3f3f3f3fLL 7 | #define OO (oo*oo) 8 | #define sz(s) (int)(s.size()) 9 | #define RT(s) return cout<> t; 51 | while (t--) { 52 | int x; 53 | cin >> m >> n >> x; 54 | cout << diceThrowWays(n, x) << endl; 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /UVa/120/12034.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | #define ll long long 5 | #define endl '\n' 6 | #define RT(v) return cout< n || n < 0 || r < 0)return 0; 26 | if (r == 0 || n == r) return 1; 27 | int& rt = Pascal[n][r]; 28 | if (~rt)return rt; 29 | return rt = (nCr(n - 1, r) + nCr(n - 1, r - 1)) % mod; 30 | } 31 | 32 | int mem[1001]; 33 | int solve(int rem) { 34 | if (rem == 0)return 1; 35 | int& rt = mem[rem]; 36 | if (~rt)return rt; 37 | rt = 0; 38 | for (int i = 1; i <= rem; i++) 39 | rt = (rt + (nCr(rem, i) * solve(rem - i)) % mod) % mod; 40 | return rt; 41 | } 42 | 43 | int main() { 44 | run(); 45 | clr(mem, -1); 46 | clr(Pascal, -1); 47 | for (int i = 0; i <= 1000; i++) 48 | for (int j = 0; j <= 1000; j++) 49 | nCr(i, j); 50 | for (int i = 1; i <= 1000; i++) 51 | solve(i); 52 | int t; cin >> t; 53 | for (int I = 1; I <= t; I++) { 54 | cout << "Case " << I << ": "; 55 | int n; cin >> n; 56 | cout << solve(n) << endl; 57 | } 58 | } -------------------------------------------------------------------------------- /DPproblems/40-MinimumCostTrainTicket.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | #define endl "\n" 6 | #define oo 0x3f3f3f3fLL 7 | #define OO (oo*oo) 8 | #define sz(s) (int)(s.size()) 9 | #define RT(s) return cout<> v; 35 | vector mem; 36 | int solve(int i) { 37 | if (i == sz(v)) return 0; 38 | int& rt = mem[i]; 39 | if (rt != oo) return rt; 40 | for (int j = i + 1; j < sz(v); j++) 41 | rt = min(rt, v[i][j] + solve(j)); 42 | return rt; 43 | } 44 | 45 | int main() { 46 | run(); 47 | int n; cin >> n; 48 | v = vector>(n, vector(n)); 49 | mem = vector(n + 1, oo); 50 | for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) 51 | cin >> v[i][j]; 52 | cout << solve(0); 53 | } 54 | -------------------------------------------------------------------------------- /DPproblems/32-SubsetSum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | #define endl "\n" 6 | #define oo 0x3f3f3f3fLL 7 | #define OO (oo*oo) 8 | #define sz(s) (int)(s.size()) 9 | #define RT(s) return cout< v; 35 | vector> mem; 36 | int subsetSum(int index, int sum) { 37 | if (sum == 0)return 1; 38 | if (sum < 0 || index == sz(v))return 0; 39 | int& rt = mem[index][sum]; 40 | if (~rt)return rt; 41 | rt = subsetSum(index + 1, sum) || subsetSum(index + 1, sum - v[index]); 42 | return rt; 43 | } 44 | 45 | int main() { 46 | run(); 47 | int n, sum; 48 | cin >> n >> sum; 49 | vector v(n); 50 | for (auto& it : v)cin >> it; 51 | mem = vector>(n, vector(sum + 1, -1)); 52 | cout << (subsetSum(0, sum) ? "YES" : "NO"); 53 | } 54 | -------------------------------------------------------------------------------- /TIMUS/1525.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | #define ll long long 6 | #define endl '\n' 7 | #define RT(v) return cout< v) { 25 | int cur = 0; 26 | int cnt1 = 0, cnt2 = m -1; 27 | for (int i = 0; i < sz(v); i++) { 28 | cur += v[i]; 29 | cnt1 = min(cnt1, cur); 30 | } 31 | cnt1 *= -1; 32 | cur = m - 1; 33 | for (int i = 0; i < sz(v); i++) { 34 | cur += v[i]; 35 | cnt2 = max(cnt2, cur); 36 | } 37 | cnt2 -= m - 1; 38 | return max(1, m - cnt1 - cnt2); 39 | } 40 | 41 | int main() { 42 | run(); 43 | int d[3]; 44 | for (int i = 0; i < 3; i++)cin >> d[i]; 45 | string s; cin >> s; 46 | vector v[3]; 47 | for (char ch : s) { 48 | if (ch == 'l')v[0].push_back(-1); 49 | else if (ch == 'r')v[0].push_back(1); 50 | else if (ch == 'u')v[1].push_back(-1); 51 | else if (ch == 'd')v[1].push_back(1); 52 | else if (ch == 'f')v[2].push_back(-1); 53 | else v[2].push_back(1); 54 | } 55 | ll ans = 1; 56 | for (int i = 0; i < 3; i++) 57 | ans *= solve(d[i], v[i]); 58 | cout << ans ; 59 | } -------------------------------------------------------------------------------- /UVa/003/389.cpp: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | #include 4 | using namespace std; 5 | typedef long long ll; 6 | typedef unsigned long long ull; 7 | typedef long double ld; 8 | typedef vector vi; 9 | #define Fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); 10 | #define endl '\n' 11 | #define sz(s) (int)(s.size()) 12 | #define all(s) s.begin(),s.end() 13 | #define allr(s) s.rbegin(),s.rend() 14 | #define RT(s,v) return cout<> 1); 25 | return r * r* power(x, y & 1); 26 | } 27 | ll conToDec(string s,int base) { 28 | ll x = 0; 29 | reverse(all(s)); 30 | for (int i = 0; i < sz(s); i++) { 31 | if (isdigit(s[i])) 32 | x += (s[i] - '0') * power(base, i); 33 | else x += (s[i] - 'A' + 10) * power(base, i); 34 | } 35 | return x; 36 | } 37 | string conFromDec(ll x, int base) { 38 | string s; 39 | if (x == 0) return "0"; 40 | while (x > 0) { 41 | if (x% base > 9) 42 | s.push_back(char(x % base + 'A' - 10)); 43 | else s.push_back(char(x % base + '0')); 44 | x /= base; 45 | } 46 | reverse(all(s)); 47 | return s; 48 | } 49 | int main() { 50 | Fast; 51 | file(); 52 | string s; 53 | int x, y; 54 | while (cin >> s >> x >> y) { 55 | ll dec = conToDec(s, x); 56 | string s = conFromDec(dec, y); 57 | cout << setw(7) << (sz(s) <= 7 ? s : "ERROR") << endl; 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /UVa/106/10611.cpp: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | #include 4 | using namespace std; 5 | typedef long long ll; 6 | typedef unsigned long long ull; 7 | typedef long double ld; 8 | typedef vector vi; 9 | #define Fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); 10 | #define endl '\n' 11 | #define sz(s) (int)(s.size()) 12 | #define all(s) s.begin(),s.end() 13 | #define allr(s) s.rbegin(),s.rend() 14 | #define RT(s,v) return cout< v; 22 | int lower(int s, int e, int x) { 23 | if (s > e) return -1; 24 | if (s == e) { 25 | if (v[s] < x) return s; 26 | return -1; 27 | } 28 | int m = s + (e - s + 1) / 2; 29 | if (v[m] < x) return lower(m, e, x); 30 | return lower(s, --m, x); 31 | } 32 | int upper(int s, int e, int x) { 33 | if (s > e) return -1; 34 | if (s == e) { 35 | if (v[s] > x) return s; 36 | return -1; 37 | } 38 | int m = (s + e) / 2; 39 | if (v[m] <= x) return upper(++m, e, x); 40 | return upper(s, m, x); 41 | } 42 | int main() { 43 | Fast; 44 | file(); 45 | int n, q; 46 | cin >> n; 47 | v.resize(n); 48 | while (n--)cin >> v[n]; 49 | sort(all(v)); 50 | cin >> q; 51 | while (q--) { 52 | int x,ind; cin >> x; 53 | ind = lower(0, sz(v) - 1, x); 54 | if (ind == -1)cout << "X" << ' '; 55 | else cout << v[ind] << ' '; 56 | ind = upper(0, sz(v) - 1, x); 57 | if (ind == -1)cout << "X" << endl; 58 | else cout << v[ind] << endl; 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /UVa/119/11995.cpp: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | #include 4 | using namespace std; 5 | typedef long long ll; 6 | typedef unsigned long long ull; 7 | typedef long double ld; 8 | typedef vector vi; 9 | #define Fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); 10 | #define endl '\n' 11 | #define sz(s) (int)(s.size()) 12 | #define all(s) s.begin(),s.end() 13 | #define allr(s) s.rbegin(),s.rend() 14 | #define exit(s) return cout<> n) { 26 | queue q; 27 | stack st; 28 | priority_queue pq; 29 | bool isSt = 1, isQ = 1, isPq = 1; 30 | while (n--) { 31 | int a, x; cin >> a >> x; 32 | if (a == 1) q.push(x), pq.push(x), st.push(x); 33 | else { 34 | if (isSt) { 35 | if (!st.empty() && st.top() == x)st.pop(); 36 | else isSt = 0; 37 | } 38 | if (isQ) { 39 | if (!q.empty() && q.front() == x)q.pop(); 40 | else isQ = 0; 41 | } 42 | if (isPq) { 43 | if (!pq.empty() && pq.top() == x)pq.pop(); 44 | else isPq = 0; 45 | } 46 | } 47 | } 48 | int cnt = isQ + isSt + isPq; 49 | if (cnt == 0) cout << "impossible" << endl; 50 | else if (cnt > 1) cout << "not sure" << endl; 51 | else { 52 | if (isSt)cout << "stack" << endl; 53 | else if (isQ) cout << "queue" << endl; 54 | else cout << "priority queue" << endl; 55 | } 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /UVa/001/127.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | #define ll long long 6 | #define endl '\n' 7 | #define RT(v) return cout< v(52); 30 | while (cin >> v[0], v[0] != "#") { 31 | for (int i = 1; i < 52; i++)cin >> v[i]; 32 | vector> st(52); 33 | for (int i = 0; i < 52; i++)st[i].push(v[i]); 34 | for (int i = 0; i < sz(st); i++) { 35 | int c = -1; 36 | if (i >= 3 && match(st[i - 3].top(), st[i].top())) { 37 | st[i - 3].push(st[i].top()); 38 | st[i].pop(); 39 | c = 3; 40 | } 41 | else if (i > 0 && match(st[i - 1].top(), st[i].top())) { 42 | st[i - 1].push(st[i].top()); 43 | st[i].pop(); 44 | c = 1; 45 | } 46 | if (st[i].empty())st.erase(st.begin() + i); 47 | i -= c + 1; 48 | } 49 | cout << sz(st) << " pile" << (sz(st) == 1 ? "" : "s") << " remaining:"; 50 | for (auto it : st) 51 | cout << ' ' << sz(it); 52 | cout << endl; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /UVa/005/574.cpp: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | #include 4 | using namespace std; 5 | typedef long long ll; 6 | typedef unsigned long long ull; 7 | #define endl '\n' 8 | #define clr(v,val) memset(v,val,sizeof(v)) 9 | #define sz(v) (int)(v.size()) 10 | #define all(v) v.begin(),v.end() 11 | #define allr(v) v.rbegin(),v.rend() 12 | #define RT(v,val) return cout< v; 26 | set < vector, greater>> out; 27 | int mx = 0; 28 | void dSum(int ind, int sum, vector st) { 29 | if (sum > t)return; 30 | if (ind >= n) { 31 | if (sum == t) { 32 | sort(allr(st)); 33 | out.insert(st); 34 | } 35 | return; 36 | } 37 | dSum(ind + 1, sum, st); 38 | st.push_back(v[ind]); 39 | dSum(ind + 1, sum + v[ind], st); 40 | } 41 | 42 | int main() { 43 | run(); 44 | while (cin >> t >> n, (t || n)) { 45 | cout << "Sums of " << t << ':' << endl; 46 | v.resize(n); 47 | out.clear(); 48 | mx = 0; 49 | for (int i = 0; i < n; i++) 50 | cin >> v[i]; 51 | dSum(0, 0, {}); 52 | if (out.empty())cout << "NONE" << endl; 53 | for (auto x : out) { 54 | cout << x[0]; 55 | for (int i = 1; i < sz(x); i++) { 56 | cout << '+' << x[i]; 57 | } 58 | cout << endl; 59 | } 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /UVa/101/10130.cpp: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | #include 4 | using namespace std; 5 | typedef long long ll; 6 | typedef unsigned long long ull; 7 | #define endl '\n' 8 | #define clr(v,val) memset(v,val,sizeof(v)) 9 | #define sz(v) (int)(v.size()) 10 | #define all(v) v.begin(),v.end() 11 | #define allr(v) v.rbegin(),v.rend() 12 | #define RT(v,val) return cout<> op; 25 | map, int> mp; 26 | int mxPrice(int w, int ind = 0) { 27 | if (ind >= n || w <= 0)return 0; 28 | if (mp.find({ w,ind }) != mp.end()) 29 | return mp[{w, ind}]; 30 | int mx = mxPrice(w, ind + 1); 31 | if (w >= op[ind].first) { 32 | mx = max(mx, op[ind].second + 33 | mxPrice(w - op[ind].first, ind + 1)); 34 | } 35 | return mp[{w, ind}] = mx; 36 | } 37 | 38 | int main() { 39 | run(); 40 | int t; cin >> t; 41 | while (t--) { 42 | mp.clear(); 43 | op.clear(); 44 | cin >> n; 45 | op.resize(n); 46 | for (int i = 0; i < n; i++) 47 | cin >> op[i].second >> op[i].first; 48 | int g; cin >> g; 49 | vector per(g); 50 | for (int i = 0; i < g; i++) 51 | cin >> per[i]; 52 | int mx = 0; 53 | for (int i = 0; i < g; i++) 54 | mx += mxPrice(per[i]); 55 | cout << mx << endl; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /UVa/113/11341.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | #define ll long long 5 | #define endl '\n' 6 | #define RT(v) return cout<> v; 23 | int mem[11][109]; 24 | int solve(int idx, int t) { 25 | if (idx == sz(v))return 0; 26 | int &rt = mem[idx][t]; 27 | if (~rt)return rt; 28 | rt = 0; 29 | for (int i = 0; i < t; i++) { 30 | if (v[idx][i] < 5)continue; 31 | int ch = solve(idx + 1, t - i - 1); 32 | if (ch >= 5 * (sz(v) - idx - 1)) 33 | rt = max(rt, v[idx][i] + ch); 34 | } 35 | return rt; 36 | } 37 | 38 | int main() { 39 | run(); 40 | cout << fixed << setprecision(2); 41 | int t; cin >> t; 42 | while (t--) { 43 | clr(mem, -1); 44 | int n, m; 45 | cin >> n >> m; 46 | v = vector>(n, vector(m)); 47 | for (int i = 0; i < n; i++) 48 | for (int j = 0; j < m; j++) 49 | cin >> v[i][j]; 50 | double ans = solve(0, m); 51 | ans = (ans + 1e-3) / n; 52 | if (ans <= 5 - 1e-3)cout << "Peter, you shouldn't have played billiard that much.\n"; 53 | else cout << "Maximal possible average mark - " << ans << ".\n"; 54 | } 55 | } -------------------------------------------------------------------------------- /UVa/121/12124.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | #define endl "\n" 6 | #define ll long long 7 | #define oo 0x3f3f3f3fLL 8 | #define sz(s) (int)(s.size()) 9 | #define RT(s) return cout<>> v; 24 | 25 | ll solve(int q) { 26 | ll cost = 0; 27 | for (auto it : v) { 28 | int mn = oo; 29 | for (auto it2 : it)if (it2.second >= q) 30 | mn = min(mn, it2.first); 31 | cost += mn; 32 | } 33 | return cost; 34 | } 35 | 36 | int main() { 37 | run(); 38 | int t; cin >> t; 39 | while (t--) { 40 | int n, b; 41 | cin >> n >> b; 42 | map >> mp; 43 | for (int i = 0; i < n; i++) { 44 | string type, name; 45 | int price, quality; 46 | cin >> type >> name >> price >> quality; 47 | mp[type].push_back({ price,quality }); 48 | } 49 | v.clear(); 50 | for (auto it : mp)v.push_back(it.second); 51 | mp.clear(); 52 | int st = 0, ed = 2e9, md, ans = 0; 53 | while (st <= ed) { 54 | md = st + ed >> 1; 55 | if (solve(md) <= b) 56 | st = md + 1, ans = md; 57 | else ed = md - 1; 58 | } 59 | cout << ans << endl; 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /UVa/001/193.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | #define endl "\n" 6 | #define ll long long 7 | #define oo 0x3f3f3f3fLL 8 | #define sz(s) (int)(s.size()) 9 | #define RT(s) return cout<> adj; 26 | vector col, rt, cur; 27 | void solve(int idx) { 28 | if (idx == sz(adj)) { 29 | if (sz(cur) > sz(rt)) rt = cur; 30 | return; 31 | } 32 | bool valid = true; 33 | for (int ch : adj[idx]) if (col[ch] == 1) { 34 | valid = false; 35 | break; 36 | } 37 | if (valid) { 38 | col[idx] = 1; 39 | cur.push_back(idx); 40 | solve(idx + 1); 41 | col[idx] = 0; 42 | cur.pop_back(); 43 | } 44 | solve(idx + 1); 45 | } 46 | 47 | int main() { 48 | run(); 49 | int t; cin >> t; 50 | while (t--) { 51 | int n, m; 52 | cin >> n >> m; 53 | adj = vector>(n + 1); 54 | col = vector(n + 1); 55 | rt.clear(); 56 | while (m--) { 57 | int u, v; cin >> u >> v; 58 | adj[max(u, v)].push_back(min(u, v)); 59 | } 60 | solve(1); 61 | cout << rt.size() << endl; 62 | for (int i = 0; i < sz(rt); i++) 63 | cout << rt[i] << " \n"[i + 1 == sz(rt)]; 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /UVa/123/12346.cpp: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | #include 4 | using namespace std; 5 | typedef long long ll; 6 | typedef unsigned long long ull; 7 | #define endl '\n' 8 | #define clr(v,val) memset(v,val,sizeof(v)) 9 | #define sz(v) (int)(v.size()) 10 | #define all(v) v.begin(),v.end() 11 | #define allr(v) v.rbegin(),v.rend() 12 | #define RT(v,val) return cout<> v; 26 | map, ll> mp; 27 | ll mnCost(int ind, ll vol) { 28 | if (ind >= n) { 29 | if (vol > 0)return oo; 30 | return 0; 31 | } 32 | if (mp.find({ ind,vol }) != mp.end()) 33 | return mp[{ind, vol}]; 34 | ll mn = mnCost(ind + 1, vol); 35 | mn = min(mn, v[ind].second + 36 | mnCost(ind + 1, vol - v[ind].first)); 37 | return mp[{ind, vol}] = mn; 38 | } 39 | 40 | int main() { 41 | run(); 42 | cin >> n; 43 | v.clear(); mp.clear(); 44 | v.resize(n); 45 | for (int i = 0; i < n; i++) 46 | cin >> v[i].first >> v[i].second; 47 | int t; cin >> t; 48 | for (int I = 1; I <= t; I++) { 49 | cout << "Case " << I << ": "; 50 | int v, x; cin >> v >> x; 51 | v = (v + x - 1) / x; 52 | ll mn = mnCost(0, v); 53 | if (mn == oo)cout << "IMPOSSIBLE" << endl; 54 | else cout << mn << endl; 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /DPproblems/5-CountNumberOfBinaryWithoutConsecutive1s.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | #define endl "\n" 6 | #define oo 0x3f3f3f3fLL 7 | #define OO (oo*oo) 8 | #define sz(s) (int)(s.size()) 9 | #define RT(s) return cout<= mod)a -= mod; 37 | } 38 | ll count(int length, bool last = 0) { 39 | if (length == 0)return 1; 40 | ll& rt = mem[length][last]; 41 | if (~rt)return rt; 42 | rt = 0; 43 | add(rt, count(length - 1, 0)); 44 | if (last == 0)add(rt, count(length - 1, 1)); 45 | return rt; 46 | } 47 | 48 | int main() { 49 | run(); 50 | clr(mem, -1); 51 | int t; cin >> t; 52 | while (t--) { 53 | int length; cin >> length; 54 | cout << count(length) << endl; 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /UVa/107/10739.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | #define endl "\n" 6 | #define oo 0x3f3f3f3fLL 7 | #define OO (oo*oo) 8 | #define sz(s) (int)(s.size()) 9 | #define RT(s) return cout< end)return 0; 39 | int& rt = mem[start][end]; 40 | if (~rt) return rt; 41 | rt = (s[start] != s[end]) + editDistance(start + 1, end - 1); 42 | rt = min({ rt, 1 + editDistance(start + 1, end) 43 | , 1 + editDistance(start, end - 1) }); 44 | return rt; 45 | } 46 | int main() { 47 | run(); 48 | int t; cin >> t; 49 | for (int I = 1; I <= t; I++) { 50 | cout << "Case " << I << ": "; 51 | clr(mem, -1); 52 | cin >> s; 53 | cout << editDistance(0, sz(s) - 1) << endl; 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /UVa/114/11413.cpp: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | #include 4 | using namespace std; 5 | typedef long long ll; 6 | typedef unsigned long long ull; 7 | typedef long double ld; 8 | typedef vector vi; 9 | #define Fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); 10 | #define endl '\n' 11 | #define sz(s) (int)(s.size()) 12 | #define all(s) s.begin(),s.end() 13 | #define allr(s) s.rbegin(),s.rend() 14 | #define RT(s,v) return cout< v; 22 | int n, m; 23 | int lower(int s, int e, int x) { 24 | if (s > e) return -1; 25 | if (s == e) { 26 | if (v[s] <= x) return s; 27 | return -1; 28 | } 29 | int m = s + (e - s + 1) / 2; 30 | if (v[m] <= x) return lower(m, e, x); 31 | return lower(s, --m, x); 32 | } 33 | int check(int x) { 34 | int cnt = 0, ind = 0; 35 | while (ind < n) { 36 | ind = lower(ind + 1, n, v[ind] + x); 37 | if (ind == -1) return -1; 38 | cnt++; 39 | } 40 | return cnt; 41 | } 42 | int main() { 43 | Fast; 44 | file(); 45 | while (cin >> n >> m) { 46 | v.resize(n + 2); 47 | for (int i = 1; i <= n; i++) 48 | cin >> v[i]; 49 | for (int i = 2; i <= n; i++) 50 | v[i] += v[i - 1]; 51 | int s = 1, e = v[n], mid, ca = 0; 52 | while (s <= e) { 53 | mid = (s + e) / 2; 54 | int cnt = check(mid); 55 | if (cnt == -1) s = mid + 1; 56 | else if (cnt > m) s = mid + 1; 57 | else ca = mid, e = mid - 1; 58 | } 59 | cout << ca << endl; 60 | } 61 | } 62 | --------------------------------------------------------------------------------